<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="不需要递归123456789101112131415161718192021222324252627282930$items = array(    1 =&amp;gt; array (&apos;id&apos; =&amp;gt; 1, &apos;pid&apos; =&amp;gt; 0, &apos;name&apos; =&amp;gt; &apos; 江西省 &apos;),    2 =&amp;gt; array (&apos;id&apos; =&amp;gt; 2, &apos;pid&apos; =&amp;gt; 0, &apos;name&apos; =&amp;gt">
<meta property="og:type" content="article">
<meta property="og:title" content="php 代码整理">
<meta property="og:url" content="http://yoursite.com/2019/08/14/php-代码整理/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="不需要递归123456789101112131415161718192021222324252627282930$items = array(    1 =&amp;gt; array (&apos;id&apos; =&amp;gt; 1, &apos;pid&apos; =&amp;gt; 0, &apos;name&apos; =&amp;gt; &apos; 江西省 &apos;),    2 =&amp;gt; array (&apos;id&apos; =&amp;gt; 2, &apos;pid&apos; =&amp;gt; 0, &apos;name&apos; =&amp;gt">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-11T09:26:57.640Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="php 代码整理">
<meta name="twitter:description" content="不需要递归123456789101112131415161718192021222324252627282930$items = array(    1 =&amp;gt; array (&apos;id&apos; =&amp;gt; 1, &apos;pid&apos; =&amp;gt; 0, &apos;name&apos; =&amp;gt; &apos; 江西省 &apos;),    2 =&amp;gt; array (&apos;id&apos; =&amp;gt; 2, &apos;pid&apos; =&amp;gt; 0, &apos;name&apos; =&amp;gt">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/08/14/php-代码整理/">



<meta name="referrer" content="never"> ​​​​


  <title>php 代码整理 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/14/php-代码整理/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">php 代码整理</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-14T20:41:02+08:00">
                2019-08-14
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  12.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  62 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="不需要递归"><a href="#不需要递归" class="headerlink" title="不需要递归"></a>不需要递归</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">$items = array(</span><br><span class="line">    <span class="number">1</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'pid'</span> =&gt; <span class="number">0</span>, <span class="string">'name'</span> =&gt; <span class="string">' 江西省 '</span>),</span><br><span class="line">    <span class="number">2</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">2</span>, <span class="string">'pid'</span> =&gt; <span class="number">0</span>, <span class="string">'name'</span> =&gt; <span class="string">' 黑龙江省 '</span>),</span><br><span class="line">    <span class="number">3</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">3</span>, <span class="string">'pid'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">' 南昌市 '</span>),</span><br><span class="line">    <span class="number">4</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">4</span>, <span class="string">'pid'</span> =&gt; <span class="number">2</span>, <span class="string">'name'</span> =&gt; <span class="string">' 哈尔滨市 '</span>),</span><br><span class="line">    <span class="number">5</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">5</span>, <span class="string">'pid'</span> =&gt; <span class="number">2</span>, <span class="string">'name'</span> =&gt; <span class="string">' 鸡西市 '</span>),</span><br><span class="line">    <span class="number">6</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">6</span>, <span class="string">'pid'</span> =&gt; <span class="number">4</span>, <span class="string">'name'</span> =&gt; <span class="string">' 香坊区 '</span>),</span><br><span class="line">    <span class="number">7</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">7</span>, <span class="string">'pid'</span> =&gt; <span class="number">4</span>, <span class="string">'name'</span> =&gt; <span class="string">' 南岗区 '</span>),</span><br><span class="line">    <span class="number">8</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">8</span>, <span class="string">'pid'</span> =&gt; <span class="number">6</span>, <span class="string">'name'</span> =&gt; <span class="string">' 和兴路 '</span>),</span><br><span class="line">    <span class="number">9</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">9</span>, <span class="string">'pid'</span> =&gt; <span class="number">7</span>, <span class="string">'name'</span> =&gt; <span class="string">' 西大直街 '</span>),</span><br><span class="line">    <span class="number">10</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">10</span>, <span class="string">'pid'</span> =&gt; <span class="number">8</span>, <span class="string">'name'</span> =&gt; <span class="string">' 东北林业大学 '</span>),</span><br><span class="line">    <span class="number">11</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">11</span>, <span class="string">'pid'</span> =&gt; <span class="number">9</span>, <span class="string">'name'</span> =&gt; <span class="string">' 哈尔滨工业大学 '</span>),</span><br><span class="line">    <span class="number">12</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">12</span>, <span class="string">'pid'</span> =&gt; <span class="number">8</span>, <span class="string">'name'</span> =&gt; <span class="string">' 哈尔滨师范大学 '</span>),</span><br><span class="line">    <span class="number">13</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">13</span>, <span class="string">'pid'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">' 赣州市 '</span>),</span><br><span class="line">    <span class="number">14</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">14</span>, <span class="string">'pid'</span> =&gt; <span class="number">13</span>, <span class="string">'name'</span> =&gt; <span class="string">' 赣县 '</span>),</span><br><span class="line">    <span class="number">15</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">15</span>, <span class="string">'pid'</span> =&gt; <span class="number">13</span>, <span class="string">'name'</span> =&gt; <span class="string">' 于都县 '</span>),</span><br><span class="line">    <span class="number">16</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">16</span>, <span class="string">'pid'</span> =&gt; <span class="number">14</span>, <span class="string">'name'</span> =&gt; <span class="string">' 茅店镇 '</span>),</span><br><span class="line">    <span class="number">17</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">17</span>, <span class="string">'pid'</span> =&gt; <span class="number">14</span>, <span class="string">'name'</span> =&gt; <span class="string">' 大田乡 '</span>),</span><br><span class="line">    <span class="number">18</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">18</span>, <span class="string">'pid'</span> =&gt; <span class="number">16</span>, <span class="string">'name'</span> =&gt; <span class="string">' 义源村 '</span>),</span><br><span class="line">    <span class="number">19</span> =&gt; array (<span class="string">'id'</span> =&gt; <span class="number">19</span>, <span class="string">'pid'</span> =&gt; <span class="number">16</span>, <span class="string">'name'</span> =&gt; <span class="string">' 上坝村 '</span>),</span><br><span class="line">);</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">genTree</span>(<span class="params">$items</span>) </span>&#123;</span><br><span class="line">    $tree = array (); <span class="comment">// 格式化好的树</span></span><br><span class="line">    foreach ($items <span class="keyword">as</span> $item)</span><br><span class="line">        <span class="keyword">if</span> (isset($items[$item[<span class="string">'pid'</span>]]))</span><br><span class="line">            $items[$item[<span class="string">'pid'</span>]][<span class="string">'son'</span>][] = &amp;$items[$item[<span class="string">'id'</span>]];</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            $tree[] = &amp;$items[$item[<span class="string">'id'</span>]];</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="插入排序算法"><a href="#插入排序算法" class="headerlink" title="插入排序算法"></a>插入排序算法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $id;</span><br><span class="line">    public $data;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/32586</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">insertSort</span>(<span class="params">&amp;$data,$n</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> ($i=<span class="number">1</span>;$i&lt;$n;$i++)&#123;</span><br><span class="line">        <span class="keyword">if</span> ($data[$i]-&gt;id&lt;$data[$i<span class="number">-1</span>]-&gt;id)&#123;</span><br><span class="line">            $j = $i<span class="number">-1</span>;</span><br><span class="line">            $x = $data[$i]-&gt;id;</span><br><span class="line">            $obj = $data[$i];</span><br><span class="line">            <span class="keyword">while</span>($j&gt;<span class="number">-1</span>&amp;&amp;$x&lt;@$data[$j]-&gt;id)&#123;</span><br><span class="line">                $data[$j+<span class="number">1</span>] = $data[$j];</span><br><span class="line">                $j--;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">            $data[$j+<span class="number">1</span>] = $obj;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    $person = <span class="keyword">new</span> Person();</span><br><span class="line">    $index = [<span class="string">'23'</span>=&gt;<span class="string">'勺颠颠'</span>,<span class="string">'65'</span>=&gt;<span class="string">'老油条'</span>,<span class="string">'21'</span>=&gt;<span class="string">'烧包谷'</span>,<span class="string">'9'</span>=&gt;<span class="string">'烧耳块'</span>,<span class="string">'4'</span>=&gt;<span class="string">'肥嘟嘟'</span>,<span class="string">'7'</span>=&gt;<span class="string">'霉戳戳'</span>,<span class="string">'32'</span>=&gt;<span class="string">'一坨肉'</span>,<span class="string">'6'</span>=&gt;<span class="string">'老扎哇'</span>];</span><br><span class="line">    $data = [];</span><br><span class="line">    foreach ($index <span class="keyword">as</span> $k=&gt;$v)&#123;</span><br><span class="line">        $obj = clone $person;</span><br><span class="line">        $obj-&gt;id = $k;</span><br><span class="line">        $obj-&gt;data = $v;</span><br><span class="line">        $data[] = $obj;</span><br><span class="line">    &#125;</span><br><span class="line">    insertSort($data,<span class="number">8</span>);</span><br><span class="line"></span><br><span class="line">    print_r($data);</span><br><span class="line"></span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>
<h3 id="分表"><a href="#分表" class="headerlink" title="分表"></a>分表</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">namespace App\Models;</span><br><span class="line"></span><br><span class="line">use DB;</span><br><span class="line">use Schema;</span><br><span class="line">use Illuminate\Database\Eloquent\Model;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Logs</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $guarded = [<span class="string">'id'</span>];</span><br><span class="line">    protected $table;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">array $attributes = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::__construct($attributes);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;table = <span class="string">'log_'</span>.date(<span class="string">'Y'</span>);</span><br><span class="line">        <span class="keyword">if</span> (!Schema::hasTable($<span class="keyword">this</span>-&gt;table))</span><br><span class="line">        &#123;</span><br><span class="line">            DB::update(<span class="string">'create table '</span>.$<span class="keyword">this</span>-&gt;table.<span class="string">' like logs'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">Logs::where(<span class="string">'uid'</span>,<span class="string">''</span>)-&gt;first();</span><br></pre></td></tr></table></figure>
<h3 id="sentry-升级"><a href="#sentry-升级" class="headerlink" title="sentry 升级"></a>sentry 升级</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen       <span class="number">80</span>;</span><br><span class="line">    server_name  track.example.com;</span><br><span class="line"></span><br><span class="line">    set_real_ip_from <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>;</span><br><span class="line">    real_ip_header X-Forwarded-For;</span><br><span class="line">    real_ip_recursive on;</span><br><span class="line"></span><br><span class="line">    location / &#123;</span><br><span class="line">        <span class="comment">// 添加这两行</span></span><br><span class="line">        default_type text/html; <span class="comment">// 设置 content-type 表示这是一个网页</span></span><br><span class="line">        return 202; # 返回 202 表示已经接收，但是并不处理</span><br><span class="line">        client_max_body_size    <span class="number">100</span>M;</span><br><span class="line">        proxy_set_header X-Real-IP  $remote_addr;</span><br><span class="line">        proxy_set_header Host-Real-IP  $http_host;</span><br><span class="line">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">        proxy_set_header X-Real-Pcol http;</span><br><span class="line">        proxy_pass http:<span class="comment">//localhost:10000;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">使用这两行，就可以保证客户端正常请求了数据，但是我却把它给抛弃了。保证客户端的正常浏览。</span><br><span class="line">cd /data/</span><br><span class="line">cp -r onpremise onpremise2</span><br><span class="line">cd onpremise</span><br><span class="line">docker-compose down</span><br><span class="line">git pull</span><br><span class="line">git diff docker-compose.yml</span><br><span class="line">git checkout docker-compose.yml</span><br><span class="line">git pull</span><br><span class="line"><span class="keyword">export</span> SENTRY_IMAGE=<span class="string">'sentry:9.1.2'</span></span><br><span class="line">docker-compose build --pull</span><br><span class="line">docker-compose run --rm web upgrade</span><br><span class="line"></span><br><span class="line">docker-compose up -d</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32569</span></span><br></pre></td></tr></table></figure>
<h3 id="基于雪花算法的-PHP-ID-生成器"><a href="#基于雪花算法的-PHP-ID-生成器" class="headerlink" title="基于雪花算法的 PHP ID 生成器"></a>基于雪花算法的 PHP ID 生成器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/godruoyi/php-snowflake</span></span><br><span class="line">$ composer <span class="built_in">require</span> godruoyi/php-snowflake -vvv</span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line"><span class="comment">// 1537200202186752</span></span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId);</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line">$snowflake-&gt;setStartTimeStamp(strtotime(<span class="string">'2019-09-09'</span>)*<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">$snowflake-&gt;id();</span><br><span class="line">集合laravel</span><br><span class="line"><span class="comment">// App\Providers\AppServiceProvider</span></span><br><span class="line"></span><br><span class="line">use Godruoyi\Snowflake\Snowflake;</span><br><span class="line">use Godruoyi\Snowflake\LaravelSequenceResolver;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AppServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register any application services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;singleton(<span class="string">'snowflake'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> (<span class="keyword">new</span> Snowflake())</span><br><span class="line">                -&gt;setStartTimeStamp(strtotime(<span class="string">'2019-08-08'</span>)*<span class="number">1000</span>)</span><br><span class="line">                -&gt;setSequenceResolver(<span class="keyword">new</span> LaravelSequenceResolver(</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;app-&gt;get(<span class="string">'cache'</span>)-&gt;store()</span><br><span class="line">                ));</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$snowflake = <span class="keyword">new</span> \Godruoyi\Snowflake\Snowflake;</span><br><span class="line">$snowflake-&gt;setSequenceResolver(<span class="function"><span class="keyword">function</span> (<span class="params">$currentTime</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> $lastTime;</span><br><span class="line">    <span class="keyword">static</span> $sequence;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($lastTime == $currentTime) &#123;</span><br><span class="line">        ++$sequence;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $lastTime = $currentTime;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $sequence;</span><br><span class="line">&#125;)-&gt;id();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32575</span></span><br></pre></td></tr></table></figure>
<h3 id="内置web服务器"><a href="#内置web服务器" class="headerlink" title="内置web服务器"></a>内置web服务器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">php -S localhost:<span class="number">8000</span> -t public/</span><br><span class="line">cd /home/baoguoxiao/www/php/demo</span><br><span class="line">php -S localhost:<span class="number">8000</span> router.php</span><br><span class="line">router.php 文件的代码</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 对URL进行解析，并获取请求的文件名</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">$uri = urldecode(parse_url($_SERVER[<span class="string">"REQUEST_URI"</span>], PHP_URL_PATH));</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是否存在该文件，如果不存在，则直接继续加载入口文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">if</span> ($uri !== <span class="string">"/"</span> &amp;&amp; file_exists(__DIR__ . <span class="string">"$uri"</span>)) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 加载入口文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">require_once <span class="string">"./index.php"</span>;</span><br><span class="line">vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 执行命令.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @return int</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @throws \Exception</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 切换路径到 public 目录</span></span><br><span class="line">    chdir(public_path());</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在命令台进行输出相关内容</span></span><br><span class="line">    $<span class="keyword">this</span>-&gt;line(<span class="string">"&lt;info&gt;Laravel development server started:&lt;/info&gt; &lt;http://&#123;$this-&gt;host()&#125;:&#123;$this-&gt;port()&#125;&gt;"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 执行外部程序，并且 $status 为系统的返回状态</span></span><br><span class="line">    passthru($<span class="keyword">this</span>-&gt;serverCommand(), $status);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// $status 为0 表示执行正常, 为其他大于0的数字表示出现了错误，有可能是端口被抢占了，这个时候就会接着判断是否进行再次尝试</span></span><br><span class="line">    <span class="keyword">if</span> ($status &amp;&amp; $<span class="keyword">this</span>-&gt;canTryAnotherPort()) &#123;</span><br><span class="line">        <span class="comment">// 对绑定的端口号加1 默认是8000, 如果失败则重试端口号为8001，再次失败重试端口号为8002，以此类推。</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;portOffset += <span class="number">1</span>;</span><br><span class="line">        <span class="comment">// 再次调用此程序</span></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;handle();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 返回状态值</span></span><br><span class="line">    <span class="keyword">return</span> $status;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取完整的 server 命令.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @return string</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">serverCommand</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> sprintf(<span class="string">'%s -S %s:%s %s'</span>,</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 获取PHP可执行命令的路径</span></span><br><span class="line">        ProcessUtils::escapeArgument((<span class="keyword">new</span> PhpExecutableFinder)-&gt;find(<span class="literal">false</span>)),</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 获取需要绑定的host</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;host(),</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取需要绑定的端口</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;port(),</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 对需要执行的参数进行转义处理。这里的 server 就是我们之前说的路由文件，它在项目的根路径下</span></span><br><span class="line">        ProcessUtils::escapeArgument(base_path(<span class="string">'server.php'</span>))</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br><span class="line">对上面的命令进行翻译一下，实际上就是执行的</span><br><span class="line"></span><br><span class="line">cd ./public</span><br><span class="line">php -S <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span>:<span class="number">8000</span> ../server.php</span><br><span class="line">note:</span><br><span class="line"></span><br><span class="line">这里我们可以看到一个区别就是之前我自己写的代码，host 都是 localhost, 但是这里写的是 <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span>。这两个有什么区别呢？</span><br><span class="line"></span><br><span class="line">其实区别很简单，比如我之前写的 localhost 绑定的ip 是 <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>, 这个相当于一个回环地址，那么我们就只允许本机的IP进行访问。而 <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span>，则表示我们对ip不进行限制，所有的IP都可以进行访问。</span><br><span class="line">那我们接着再来看看项目根目录下面的server.php:</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Laravel - A PHP Framework For Web Artisans</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @package  Laravel</span></span><br><span class="line"><span class="comment"> * @author   Taylor Otwell &lt;taylor@laravel.com&gt;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line">$uri = urldecode(</span><br><span class="line">    parse_url($_SERVER[<span class="string">'REQUEST_URI'</span>], PHP_URL_PATH)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这个文件允许我们从内置 PHP web 服务器中模拟 Apache 的 "mod_rewrite" 功能.</span></span><br><span class="line"><span class="comment">// 这提供了一种测试 Laravel 应用程序的便捷方法,</span></span><br><span class="line"><span class="comment">// 而无需在此安装"真正的" web 服务器软件。</span></span><br><span class="line"><span class="keyword">if</span> ($uri !== <span class="string">'/'</span> &amp;&amp; file_exists(__DIR__.<span class="string">'/public'</span>.$uri)) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">require_once __DIR__.<span class="string">'/public/index.php'</span>;</span><br><span class="line">https:<span class="comment">//github.com/mowangjuanzi/blog/blob/master/posts/2019.05.18-php_built_in_web_server.md</span></span><br></pre></td></tr></table></figure>
<h3 id="响应宏"><a href="#响应宏" class="headerlink" title="响应宏"></a>响应宏</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">在任意一个ServiceProvider的boot方法里（ResponseMacroServiceProvider），使用Response Facade注册</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Response::macro(<span class="string">'success'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$data = [], $message = <span class="string">'success'</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> JsonResponse([</span><br><span class="line">        <span class="string">'code'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">        <span class="string">'data'</span> =&gt; $data,</span><br><span class="line">        <span class="string">'message'</span> =&gt; $message</span><br><span class="line">    ], <span class="number">200</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">接下来， 你可以再任何地方使用它response()。</span><br><span class="line"></span><br><span class="line"><span class="comment">//UserController.php</span></span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">users</span>(<span class="params">UserRepository $userRepository</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> response()-&gt;success($userRepository-&gt;all(), <span class="string">'success'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">注意，你只能通过response()这个全局方法或是app(<span class="string">'Illuminate\Routing\ResponseFactory'</span>)来使用它</span><br><span class="line"></span><br><span class="line">response()-&gt;success();<span class="comment">//OK</span></span><br><span class="line">app(<span class="string">'Illuminate\Routing\ResponseFactory'</span>)-&gt;success();<span class="comment">//OK</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//Response Facade</span></span><br><span class="line">Response::success();<span class="comment">//ok</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> \Illuminate\Http\Response)-&gt;success();<span class="comment">//Error</span></span><br><span class="line">https:<span class="comment">//godruoyi.com/posts/laravel-response-macro-principle</span></span><br></pre></td></tr></table></figure>
<h3 id="Intervention-Image-合成图片"><a href="#Intervention-Image-合成图片" class="headerlink" title="Intervention Image 合成图片"></a>Intervention Image 合成图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">use Intervention\Image\ImageManager;</span><br><span class="line"></span><br><span class="line">$manager = <span class="keyword">new</span> ImageManager(array(<span class="string">'driver'</span> =&gt; <span class="string">'imagick'</span>));</span><br><span class="line">use Intervention\Image\Facades\Image;</span><br><span class="line"></span><br><span class="line"><span class="comment">//或</span></span><br><span class="line">$manager = app(<span class="string">'image'</span>);</span><br><span class="line"><span class="comment">// $image = $manager-&gt;make($bg);</span></span><br><span class="line"><span class="comment">// $image = Image::make($bg);</span></span><br><span class="line"></span><br><span class="line">$image = app(<span class="string">'image'</span>)-&gt;make($bg);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 二维码图片</span></span><br><span class="line">$qrcodeImage = app(<span class="string">'image'</span>)-&gt;make($qrcodeurl)-&gt;resize(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line"><span class="comment">// 重置 头像 大小</span></span><br><span class="line">$avatarImage  = app(<span class="string">'image'</span>)-&gt;make($httpClient-&gt;request(<span class="string">'GET'</span>, $avatarurl)-&gt;getBody())-&gt;resize(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line"></span><br><span class="line">$image-&gt;text(<span class="string">'Nickname'</span>, <span class="number">376</span>, <span class="number">320</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$font</span>) </span>&#123;</span><br><span class="line">    $font-&gt;file(public_path(<span class="string">'fonts/SimHei.ttf'</span>));</span><br><span class="line">    $font-&gt;size(<span class="number">40</span>);</span><br><span class="line">    $font-&gt;color(<span class="string">'#000000'</span>);</span><br><span class="line">    $font-&gt;align(<span class="string">'center'</span>);</span><br><span class="line">    $font-&gt;valign(<span class="string">'top'</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$image-&gt;insert($qrcodeImage, <span class="string">'bottom'</span>, <span class="number">0</span>, <span class="number">360</span>);</span><br><span class="line">$image-&gt;insert($avatarImage, <span class="string">'top'</span>, <span class="number">0</span>, <span class="number">105</span>);</span><br><span class="line">中文字体文件https:<span class="comment">//github.com/StellarCN/scp_zh/tree/master/fonts</span></span><br></pre></td></tr></table></figure>
<h3 id="nginx-配置多域名"><a href="#nginx-配置多域名" class="headerlink" title="nginx 配置多域名"></a>nginx 配置多域名</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">这个是精确匹配问题：</span><br><span class="line"></span><br><span class="line">nginx server_name 匹配不到时，默认取第一个 server &#123; &#125; (端口和 ip 一致)</span><br><span class="line"></span><br><span class="line">你项目中配置:</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> server_name www.test.com</span><br><span class="line"><span class="number">2.</span> server_name www.mytest.com</span><br><span class="line">访问 test.com/mytest.com 都找不到对应的 server_name (www.test.com != test.com , www.mytest.com != mytest.com)，所以就取第一个 server &#123; &#125; 匹配，所以均输出 test</span><br><span class="line"></span><br><span class="line">server_name mytest.com *.mytest.com</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/32300</span></span><br></pre></td></tr></table></figure>
<h3 id="插入折半排序"><a href="#插入折半排序" class="headerlink" title="插入折半排序"></a>插入折半排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $id;</span><br><span class="line">    public $data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">insertSort</span>(<span class="params">&amp;$data,$n</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> ($i=<span class="number">1</span>;$i&lt;$n;$i++)&#123;</span><br><span class="line">        $low = <span class="number">0</span>;</span><br><span class="line">        $high = $i<span class="number">-1</span>;</span><br><span class="line">        $temp = $data[$i];</span><br><span class="line">        <span class="keyword">while</span> ($low&lt;=$high)&#123;</span><br><span class="line">            $mid = floor(($low+$high)/<span class="number">2</span>);</span><br><span class="line">            <span class="keyword">if</span> ($data[$mid]-&gt;id&gt;$temp-&gt;id)&#123;</span><br><span class="line">                $high = $mid<span class="number">-1</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                $low = $mid+<span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> ($j=$i;$j&gt;$low;$j--)&#123;</span><br><span class="line">            $data[$j] = $data[$j<span class="number">-1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        $data[$low] = $temp;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    $person = <span class="keyword">new</span> Person();</span><br><span class="line">    $index = [<span class="string">'23'</span>=&gt;<span class="string">'勺颠颠'</span>,<span class="string">'65'</span>=&gt;<span class="string">'老油条'</span>,<span class="string">'21'</span>=&gt;<span class="string">'烧包谷'</span>,<span class="string">'9'</span>=&gt;<span class="string">'烧耳块'</span>,<span class="string">'4'</span>=&gt;<span class="string">'肥嘟嘟'</span>,<span class="string">'7'</span>=&gt;<span class="string">'霉戳戳'</span>,<span class="string">'32'</span>=&gt;<span class="string">'一坨肉'</span>,<span class="string">'6'</span>=&gt;<span class="string">'老扎哇'</span>];</span><br><span class="line">    $data = [];</span><br><span class="line">    foreach ($index <span class="keyword">as</span> $k=&gt;$v)&#123;</span><br><span class="line">        $obj = clone $person;</span><br><span class="line">        $obj-&gt;id = $k;</span><br><span class="line">        $obj-&gt;data = $v;</span><br><span class="line">        $data[] = $obj;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    insertSort($data,<span class="number">8</span>);</span><br><span class="line">    print_r($data);</span><br><span class="line"></span><br><span class="line">&#125;)(); </span><br><span class="line">https:<span class="comment">//learnku.com/articles/32691</span></span><br></pre></td></tr></table></figure>
<h3 id="sql拼接"><a href="#sql拼接" class="headerlink" title="sql拼接"></a>sql拼接</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$statusInStr = implode(<span class="string">','</span>, <span class="string">'1,2,3'</span>);</span><br><span class="line">字符串</span><br><span class="line">$statusInStr = <span class="string">"'"</span>.implode(<span class="string">"','"</span>, [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]).<span class="string">"'"</span>;<span class="comment">//'1','2','3'</span></span><br><span class="line"><span class="string">"select * from table where status in (&#123;$statusInStr&#125;)"</span><span class="comment">//select * from table where status in ('1','2','3')</span></span><br></pre></td></tr></table></figure>
<h3 id="x-www-form-urlencoded-与-multipart-form-data"><a href="#x-www-form-urlencoded-与-multipart-form-data" class="headerlink" title="x-www-form-urlencoded 与 multipart/form-data"></a>x-www-form-urlencoded 与 multipart/form-data</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">x-www-form-urlencoded，表单默认的 Content-type 类型，支持 ASCII-text 文本内容</span><br><span class="line">multipart/form-data，允许提交表单包含： files，non-ASCII-text，Binary 类型数据</span><br><span class="line">当表单使用 application/x-www-form-urlencoded 时，需要对参数进行 urlencode 编码和序列化</span><br><span class="line"></span><br><span class="line">如，表单提交参数（key-value）为：</span><br><span class="line"></span><br><span class="line">param1:website</span><br><span class="line">param2:https:<span class="comment">//www.google.com</span></span><br><span class="line">经过 urlencode 编码后：</span><br><span class="line"></span><br><span class="line">param1:website</span><br><span class="line">param2:https%<span class="number">3</span>A%<span class="number">2</span>F%<span class="number">2</span>Fwww.google.com</span><br><span class="line">再经过序列化，得到结果</span><br><span class="line"></span><br><span class="line">param1=website&amp;param2=https%<span class="number">3</span>A%<span class="number">2</span>F%<span class="number">2</span>Fwww.google.com</span><br><span class="line">multipart/form-data</span><br><span class="line"></span><br><span class="line">一个 multipart/form-data 消息体，包含多个块组成，每个块代表一个有效的表单控件，并使用 boundary 的字符串分割：</span><br><span class="line"></span><br><span class="line">第一部分，Content-Disposition: form-data 参数名称，如，name=<span class="string">"my_control</span></span><br><span class="line"><span class="string">第二部分，Content-Type: text/plain，Content-Type: image/png</span></span><br><span class="line"><span class="string">第三部分，消息内容</span></span><br><span class="line"><span class="string">例如表单：</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;FORM action="</span>http:<span class="comment">//server.com/cgi/handle"</span></span><br><span class="line">       enctype=<span class="string">"multipart/form-data"</span></span><br><span class="line">       method=<span class="string">"post"</span>&gt;</span><br><span class="line">   &lt;P&gt;</span><br><span class="line">   What is your name? &lt;INPUT type="text" name="submit-name"&gt;&lt;BR&gt;</span><br><span class="line">   What files are you sending? &lt;INPUT type="file" name="files"&gt;&lt;BR&gt;</span><br><span class="line">   &lt;INPUT type="submit" value="Send"&gt;</span><br><span class="line">   &lt;INPUT type="reset"&gt;</span><br><span class="line">&lt;/FORM&gt;</span><br><span class="line">假设，submit-name 输入 Larry 文本，files 选择文件 file1.txt</span><br><span class="line"></span><br><span class="line">Content-Type: multipart/form-data; boundary=AaB03x</span><br><span class="line"></span><br><span class="line">--AaB03x</span><br><span class="line">Content-Disposition: form-data; name="submit-name"</span><br><span class="line"></span><br><span class="line">Larry</span><br><span class="line">--AaB03x</span><br><span class="line">Content-Disposition: form-data; name="files"; filename="file1.txt"</span><br><span class="line">Content-Type: text/plain</span><br><span class="line"></span><br><span class="line">... contents of file1.txt ...</span><br><span class="line">--AaB03x--</span><br><span class="line">https://learnku.com/articles/32812</span><br></pre></td></tr></table></figure>
<h3 id="Model-setAttribute"><a href="#Model-setAttribute" class="headerlink" title="Model::setAttribute()"></a>Model::setAttribute()</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">App\GameWork::create([</span><br><span class="line">    <span class="string">"task_id"</span> =&gt; <span class="string">"23571"</span>,</span><br><span class="line">    <span class="string">"type"</span> =&gt; <span class="number">2</span>,</span><br><span class="line">    <span class="string">"take_time"</span> =&gt; <span class="string">"1563085291"</span>,</span><br><span class="line">    <span class="string">"send_time"</span> =&gt; <span class="number">1563093004</span>,</span><br><span class="line">    <span class="string">"chufa"</span> =&gt; <span class="number">10</span>,</span><br><span class="line">    <span class="string">"worker"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">    <span class="string">"work_time"</span> =&gt; <span class="number">0</span>,</span><br><span class="line">    <span class="string">"log"</span> =&gt; <span class="string">"超时"</span>,</span><br><span class="line">    <span class="string">"uid"</span> =&gt; <span class="number">271</span>,</span><br><span class="line">    <span class="string">"game_id"</span> =&gt; <span class="number">3532054</span>,</span><br><span class="line">    <span class="string">"buyer_id"</span> =&gt; <span class="number">64</span></span><br><span class="line">]);</span><br><span class="line">结果提示我</span><br><span class="line"></span><br><span class="line"><span class="built_in">TypeError</span>: Too few <span class="built_in">arguments</span> to <span class="function"><span class="keyword">function</span> <span class="title">Illuminate</span>/<span class="title">Database</span>/<span class="title">Eloquent</span>/<span class="title">Model</span>::<span class="title">setAttribute</span>(<span class="params"></span>), 1 <span class="title">passed</span> <span class="title">in</span> /<span class="title">Library</span>/<span class="title">WebServer</span>/<span class="title">Documents</span>/<span class="title">api</span>/<span class="title">vendor</span>/<span class="title">laravel</span>/<span class="title">framework</span>/<span class="title">src</span>/<span class="title">Illuminate</span>/<span class="title">Database</span>/<span class="title">Eloquent</span>/<span class="title">Concerns</span>/<span class="title">HasAttributes</span>.<span class="title">php</span> <span class="title">on</span> <span class="title">line</span> 617 <span class="title">and</span> <span class="title">exactly</span> 2 <span class="title">expected</span></span></span><br><span class="line"><span class="function">把自增关闭就好了，<span class="title">laravel</span> 默认是所有表都有个自增 <span class="title">ID</span>，我的表不需要</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">public</span> <span class="title">$incrementing</span> = <span class="title">false</span>;</span></span><br><span class="line"><span class="function"><span class="title">https</span>://<span class="title">learnku</span>.<span class="title">com</span>/<span class="title">laravel</span>/<span class="title">t</span>/31164</span></span><br></pre></td></tr></table></figure>
<h3 id="处理unicode解码"><a href="#处理unicode解码" class="headerlink" title="处理unicode解码"></a>处理unicode解码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// change unicode to unt-8</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">replace_unicode_escape_sequence</span>(<span class="params">$match</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> mb_convert_encoding(pack(<span class="string">'H*'</span>, $match[<span class="number">1</span>]), <span class="string">'UTF-8'</span>, <span class="string">'UCS-2BE'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unicode_decode</span>(<span class="params">$str</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> preg_replace_callback(<span class="string">'/u([0-9a-f]&#123;4&#125;)/i'</span>, <span class="string">'replace_unicode_escape_sequence'</span>, $str); </span><br><span class="line">&#125;</span><br><span class="line">$str = unicode_decode(<span class="string">'&#123;"u5173u952eu8bcd":[&#123;"","key":"u767eu5ea6"&#125;]&#125;'</span>);</span><br><span class="line"><span class="comment">//&#123;"关键词":[&#123;"","key":"百度"&#125;]&#125;</span></span><br><span class="line">如果编码是 \u5173\u952e\u8bcd 正则改为/\u([<span class="number">0</span><span class="number">-9</span>a-f]&#123;<span class="number">4</span>&#125;)/i 即可</span><br><span class="line">http:<span class="comment">//cuihuan.net/2015/11/28/php%20%E5%A4%84%E7%90%86unicode%E8%A7%A3%E7%A0%81/</span></span><br></pre></td></tr></table></figure>
<h3 id="恶意提交攻击"><a href="#恶意提交攻击" class="headerlink" title="恶意提交攻击"></a>恶意提交攻击</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// check 环境ip</span></span><br><span class="line">    <span class="keyword">if</span> (!$<span class="keyword">this</span>-&gt;isWhiteList($ip)) &#123;</span><br><span class="line">        $echo_str = <span class="string">"提交过于频繁,请稍后再试！"</span>;</span><br><span class="line">        <span class="comment">// 构建ip的时间栈数据</span></span><br><span class="line">        <span class="keyword">if</span> (!is_array($_SESSION[$ip])) &#123;</span><br><span class="line">            $_SESSION[$ip] = array();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">0</span>])) &#123;</span><br><span class="line">            $_SESSION[$ip][] = time();</span><br><span class="line">            <span class="comment">// session 保存时间为6小时。清理session</span></span><br><span class="line">            $post_interval_first = time() - $_SESSION[$ip][<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">if</span> ($post_interval_first &gt; <span class="number">21600</span>) &#123;</span><br><span class="line">                $_SESSION[$ip] = array();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 两次提交小于1s，禁止提交</span></span><br><span class="line">            $post_interval_pre = time() - $_SESSION[$ip][count($_SESSION[$ip]) - <span class="number">3</span>];</span><br><span class="line">            <span class="keyword">if</span> ($post_interval_pre &lt; <span class="number">1</span>) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;;</span><br><span class="line">            <span class="comment">// 您在10s内已经提交了3请求，禁止提交</span></span><br><span class="line">            $post_interval_third = time() - $_SESSION[$ip][count($_SESSION[$ip]) - <span class="number">3</span>];</span><br><span class="line">            <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">3</span>]) &amp;&amp; ($post_interval_third &lt; <span class="number">10</span>)) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 您在1分钟期间已经提交了5请求，禁止提交</span></span><br><span class="line">            $post_interval_fifth = time() - $_SESSION[$ip][count($_SESSION[$ip]) - <span class="number">3</span>];</span><br><span class="line">            <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">5</span>]) &amp;&amp; ($post_interval_fifth &lt; <span class="number">60</span>)) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 6小时内提交10次，禁止提交</span></span><br><span class="line">            <span class="keyword">if</span> (isset($_SESSION[$ip][<span class="number">10</span>])) &#123;</span><br><span class="line">                echo $echo_str;</span><br><span class="line">                exit;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $_SESSION[$ip][] = time();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isWhiteList</span>(<span class="params">$ip</span>)</span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 内网ip默认全部存在于白名单中</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">if</span>(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 是否在写死的whitelist 里面</span></span><br><span class="line">    <span class="keyword">return</span> in_array($ip,$<span class="keyword">this</span>-&gt;_WHTTE_LIST);</span><br><span class="line">&#125;</span><br><span class="line">http:<span class="comment">//cuihuan.net/2016/01/07/%E6%9C%BA%E5%99%A8%E5%A4%9A%E6%AC%A1%E6%81%B6%E6%84%8F%E6%8F%90%E4%BA%A4%E6%94%BB%E5%87%BB/</span></span><br></pre></td></tr></table></figure>
<h3 id="切换镜像"><a href="#切换镜像" class="headerlink" title="切换镜像"></a>切换镜像</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">composer config -g repo.packagist composer https:<span class="comment">//mirrors.aliyun.com/composer/</span></span><br><span class="line">composer update -vvv</span><br><span class="line"></span><br><span class="line">执行成功以后，打开 composer.lock, 发现文件中的链接依旧是 laravel-china 的地址</span><br><span class="line"></span><br><span class="line">删掉 lock，再 install，会安装最新的包，在生产环境这样，可能会搞出事来，，</span><br><span class="line"></span><br><span class="line">composer update nothing 直接这样，就可以只更新 lock 文件里的源</span><br></pre></td></tr></table></figure>
<h3 id="composer-Failed-to-execute-unzip"><a href="#composer-Failed-to-execute-unzip" class="headerlink" title="composer Failed to execute unzip"></a>composer Failed to execute unzip</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> composer 多线程下载的扩展包（hirak/prestissimo），因为是全局安装的，其原理就是下到缓存，然后以后全部都走缓存安装。所以我第一件事 先全局卸载掉它</span><br><span class="line"></span><br><span class="line">composer global remove hirak/prestissimo</span><br><span class="line">然后清空当前 composer 的缓存</span><br><span class="line"></span><br><span class="line">composer clear-cache</span><br><span class="line">然后使用 laravel <span class="keyword">new</span> blog， 你会发现没有报错了。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32898</span></span><br></pre></td></tr></table></figure>
<h3 id="sentry"><a href="#sentry" class="headerlink" title="sentry"></a>sentry</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$ git clone https:<span class="comment">//github.com/getsentry/onpremise.git</span></span><br><span class="line">$ cd onpremise</span><br><span class="line">$ ./install.sh</span><br><span class="line">$ docker-compose up -d</span><br><span class="line"></span><br><span class="line">在目录下的 docker-compose.yml 添加配置：</span><br><span class="line"></span><br><span class="line">SENTRY_EMAIL_HOST: smtp.exmail.qq.com</span><br><span class="line">SENTRY_EMAIL_USER: 你的邮箱地址</span><br><span class="line">SENTRY_EMAIL_EMAIL: 你的邮箱地址</span><br><span class="line">SENTRY_EMAIL_PASSWORD: 授权码</span><br><span class="line">SENTRY_EMAIL_USE_TLS: <span class="string">'true'</span></span><br><span class="line">SENTRY_EMAIL_PORT: <span class="number">587</span></span><br><span class="line">重启 sentry 服务</span><br><span class="line"></span><br><span class="line">docker-compose down &amp;&amp; docker-compose up -d</span><br></pre></td></tr></table></figure>
<h3 id="抽离-vendor"><a href="#抽离-vendor" class="headerlink" title="抽离 vendor"></a>抽离 vendor</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">在 bootstrap 注册 autoload 就可以了。</span><br><span class="line">./bootstrap/app.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">require_once __DIR__ . <span class="string">'/../vendor/autoload.php'</span>;</span><br><span class="line"></span><br><span class="line">$autoload = <span class="keyword">new</span> Composer\Autoload\ClassLoader();</span><br><span class="line">$autoload-&gt;addPsr4(<span class="string">'App\\'</span>, <span class="string">"../app/"</span>);</span><br><span class="line">$autoload-&gt;addClassMap([</span><br><span class="line">    <span class="string">"database/"</span>,</span><br><span class="line">    <span class="string">"tests/"</span></span><br><span class="line">]);</span><br><span class="line">$autoload-&gt;register();</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/32840</span></span><br></pre></td></tr></table></figure>
<h3 id="dd-函数"><a href="#dd-函数" class="headerlink" title="dd 函数"></a>dd 函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">dd</span>(<span class="params">...$args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        header(<span class="string">'Access-Control-Allow-Origin: *'</span>);</span><br><span class="line">        header(<span class="string">'Access-Control-Allow-Methods: *'</span>);</span><br><span class="line">        header(<span class="string">'Access-Control-Allow-Headers: *'</span>);</span><br><span class="line">        http_response_code(<span class="number">500</span>);</span><br><span class="line"> </span><br><span class="line">        foreach ($args <span class="keyword">as</span> $x) &#123;</span><br><span class="line">            (<span class="keyword">new</span> Illuminate\Support\Debug\Dumper)-&gt;dump($x);</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        die(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="listen-sql"><a href="#listen-sql" class="headerlink" title="listen sql"></a>listen sql</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">\DB::listen(<span class="function"><span class="keyword">function</span> (<span class="params">QueryExecuted $sql</span>) </span>&#123;</span><br><span class="line">     \Log::info($sql-&gt;sql);</span><br><span class="line">     \Log::info((<span class="keyword">new</span> \Exception())-&gt;getTraceAsString());</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="模型事件-updated"><a href="#模型事件-updated" class="headerlink" title="模型事件 updated"></a>模型事件 updated</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">只有 $sku-&gt;&#123;attribute&#125; != $sku-&gt;getOriginal(&#123;attribute&#125;) 不一致的时候才会触发</span><br><span class="line">　　getDirty() 不为空的时候才触发, 而且不会比较数据类型(判断是否 dirty 使用的是 == 而不是 ===)</span><br><span class="line"></span><br><span class="line">直接 app(Model::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">where</span>()-&gt;<span class="title">update</span>() 不会触发</span></span><br><span class="line"><span class="class">　　$<span class="title">sku</span> </span>= app(Sku::<span class="class"><span class="keyword">class</span>), $<span class="title">sku</span>-&gt;<span class="title">has_stock</span> </span>= <span class="number">1</span>; $sku-&gt;save() 这样才会触发</span><br><span class="line"></span><br><span class="line">这是因为，使用 app(Model::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">where</span>()-&gt;<span class="title">update</span>() 的时候，调用的是 <span class="title">Query</span>/<span class="title">Builder</span> 的 <span class="title">update</span> 方法，这里面是没法触发 <span class="title">update</span> 事件的。</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">使用 $<span class="title">sku</span> </span>= app(Sku::<span class="class"><span class="keyword">class</span>), $<span class="title">sku</span>-&gt;<span class="title">has_stock</span> </span>= <span class="number">1</span>; $sku-&gt;save() 的时候，使用的 update 方法是 Eloquent/Model 里面的 update 方法。</span><br><span class="line">https:<span class="comment">//blog.baiguiren.com/2019/04/22/laravel/Laravel%20%E6%A8%A1%E5%9E%8B%E4%BA%8B%E4%BB%B6%20updated%20%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/</span></span><br></pre></td></tr></table></figure>
<h3 id="获取所有-collection"><a href="#获取所有-collection" class="headerlink" title="获取所有 collection"></a>获取所有 collection</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">foreach (\DB::connection(<span class="string">'xxx'</span>)-&gt;getMongoDB()-&gt;listCollections() <span class="keyword">as</span> $collection) &#123;</span><br><span class="line">    echo $collection-&gt;getName() . PHP_EOL;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取带参数的-sql-语句"><a href="#获取带参数的-sql-语句" class="headerlink" title="获取带参数的 sql 语句"></a>获取带参数的 sql 语句</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">DB::table(<span class="string">'users'</span>)-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>)-&gt;toSql();</span><br><span class="line">select * <span class="keyword">from</span> <span class="string">`users`</span> where <span class="string">`id`</span> = ?</span><br><span class="line">$builder = DB::table(<span class="string">'users'</span>)-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>);</span><br><span class="line">$bindings = $builder-&gt;getBindings();</span><br><span class="line">$sql = str_replace(<span class="string">'?'</span>, <span class="string">'%s'</span>, $builder-&gt;toSql());</span><br><span class="line">$sql = sprintf($sql, ...$bindings);</span><br><span class="line">select * <span class="keyword">from</span> <span class="string">`tb_user`</span> where <span class="string">`id`</span> = <span class="number">1</span></span><br><span class="line"></span><br><span class="line">\Illuminate\Database\Query\Builder::macro(<span class="string">'sql'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $bindings = $<span class="keyword">this</span>-&gt;getBindings();</span><br><span class="line">    $sql = str_replace(<span class="string">'?'</span>, <span class="string">'%s'</span>, $<span class="keyword">this</span>-&gt;toSql());</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> sprintf($sql, ...$bindings);</span><br><span class="line">&#125;);</span><br><span class="line">dd(DB::table(<span class="string">'users'</span>)-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>)-&gt;sql());</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//blog.baiguiren.com/2018/06/27/laravel/Laravel%20%E4%B8%AD%E7%9A%84%20toSql%20%E8%8E%B7%E5%8F%96%E5%B8%A6%E5%8F%82%E6%95%B0%E7%9A%84%20sql%20%E8%AF%AD%E5%8F%A5/</span></span><br></pre></td></tr></table></figure>
<h3 id="命令行输出进度条"><a href="#命令行输出进度条" class="headerlink" title="命令行输出进度条"></a>命令行输出进度条</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">namespace App\Commands;</span><br><span class="line"> </span><br><span class="line">use Illuminate\Console\Command;</span><br><span class="line">use Symfony\Component\Console\Helper\ProgressBar;</span><br><span class="line">use Symfony\Component\Console\Output\ConsoleOutput;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test</span> <span class="keyword">extends</span> <span class="title">Command</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $signature = <span class="string">'test'</span>;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute the command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">        $output = <span class="keyword">new</span> ConsoleOutput();</span><br><span class="line">        $progressBar = <span class="keyword">new</span> ProgressBar($output, <span class="number">1000</span>);</span><br><span class="line">        $progressBar-&gt;setFormat(<span class="string">"   %elapsed:6s%/%estimated:-6s%   内存消耗: %memory:6s%\n%current%/%max% [%bar%] %percent:3s%%"</span>);</span><br><span class="line"> </span><br><span class="line">        foreach (range(<span class="number">1</span>, <span class="number">1000</span>) <span class="keyword">as</span> $_) &#123;</span><br><span class="line">            usleep(<span class="number">5000</span>);</span><br><span class="line">            $progressBar-&gt;advance();</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        $progressBar-&gt;finish();</span><br><span class="line">        echo <span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$progressBar-&gt;start();</span><br><span class="line">print <span class="string">"\n"</span>;</span><br><span class="line">$progressBar-&gt;start();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$progressBar-&gt;setFormat(<span class="string">"已处理数量: %count%"</span>);  <span class="comment">// 这里是一个占位符，可以和进度条写在一起</span></span><br><span class="line">$progressBar-&gt;setMessage($count, <span class="string">'count'</span>); <span class="comment">// 使用 `$count` 替换输出内容中的 "%$count%"</span></span><br></pre></td></tr></table></figure>
<h3 id="with-通过闭包筛选"><a href="#with-通过闭包筛选" class="headerlink" title="with 通过闭包筛选"></a>with 通过闭包筛选</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">profile</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasOne(<span class="string">'App\UserProfile'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">App\User::<span class="keyword">with</span>([</span><br><span class="line">    <span class="string">'profile'</span> =&gt; <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>) </span>&#123;</span><br><span class="line">        $query-&gt;select([<span class="string">'id'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">])</span><br><span class="line">-&gt;find(<span class="number">4</span>)</span><br><span class="line">-&gt;toArray();</span><br><span class="line">即使数据库有记录，sql 也记录了对应的查询语句，但是 profile 关联却是空的</span><br><span class="line">App\User::<span class="keyword">with</span>([</span><br><span class="line">    <span class="string">'profile'</span> =&gt; <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>) </span>&#123;</span><br><span class="line">        $query-&gt;select([<span class="string">'id'</span>, <span class="string">'user_id'</span>]); <span class="comment">// 多了 user_id</span></span><br><span class="line">    &#125;</span><br><span class="line">])</span><br><span class="line">-&gt;find(<span class="number">4</span>)</span><br><span class="line">-&gt;toArray();</span><br><span class="line">可以查找到正确的 profile 了</span><br><span class="line">hasMany 方法默认第二第三个参数其实就是这两个集合建立关联的关键，第三个参数 user_id、第四个参数 id；这样一来我们就可以通过比较 Profile 的 user_id 和 User 里面的 id，如果相等，则这个 Profile 是属于这个 User 的，我们就把该 Profile 放进 User 的 profile 关联中，最后就得到我们想要的结果了</span><br><span class="line">Laravel 先查询主要的数据(不带 <span class="keyword">with</span>)，查询完了之后，取出其中的 id 列数组(不一定都是id啊，只是举个例子)，将这个数组作为条件去查找关联，有多少个关联就会再去查找多少次，查找完关联之后通过得到的结果的主键和关联数据的外键比对，相等则建立关联。</span><br><span class="line"></span><br><span class="line">总结：在关联筛选 field 的时候，也必须要把关联的外键写进去，否则，即使产生了正确的 sql 语句，但是它们建立不了关联，通过 $user-&gt;profile 得到的还是一个空集合。</span><br><span class="line">https:<span class="comment">//blog.baiguiren.com/2018/01/06/laravel/Laravel%205.1%20Eloquent%20with%20%E9%80%9A%E8%BF%87%E9%97%AD%E5%8C%85%E7%AD%9B%E9%80%89%E7%89%B9%E5%AE%9A%20field%20%E5%BE%97%E4%B8%8D%E5%88%B0%E7%BB%93%E6%9E%9C%E7%9A%84%E9%97%AE%E9%A2%98/</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-cookie加密解密"><a href="#Laravel-cookie加密解密" class="headerlink" title="Laravel cookie加密解密"></a>Laravel cookie加密解密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$data = <span class="string">'laravel'</span>;</span><br><span class="line">$iv = random_bytes(<span class="number">16</span>);</span><br><span class="line">$key = <span class="string">'this is key'</span>;</span><br><span class="line"> </span><br><span class="line">$encrypt = openssl_encrypt($data, <span class="string">'AES-256-CBC'</span>, $key, <span class="number">0</span>, $iv);</span><br><span class="line"> </span><br><span class="line">var_dump($encrypt);</span><br><span class="line">var_dump(openssl_decrypt($encrypt, <span class="string">'AES-256-CBC'</span>, $key, <span class="number">0</span>, $iv));</span><br><span class="line">Laravel 中的话，key 就是 .env 配置文件里面的 APP_KEY，除了 key 还有两个变化的参数就是 加密、解密的数据以及 iv。</span><br><span class="line"></span><br><span class="line">也就是说，如果我们需要加密 cookie 的话，我们至少得保存下 加密后的数据以及 iv</span><br><span class="line">虽然每次请求 cookie 都会发生变化，但是实际数据是没有变的，发生变化只是因为用来加密的 iv 变了(使用 random_bytes 方法生成)。</span><br><span class="line"></span><br><span class="line">由于 iv 每次都变化，所以需要把 iv 也一同返回给浏览器，加上验证数据合法性的 mac，最后返回的就是下面的数组的 json 编码后在 base64 编码的数据：</span><br><span class="line">[</span><br><span class="line">   <span class="string">'iv'</span> =&gt; random_bytes(<span class="number">16</span>), <span class="comment">// 16位随机字节串</span></span><br><span class="line">   <span class="string">'value'</span> =&gt; <span class="string">'xxxx...'</span>,    <span class="comment">// 加密后的数据</span></span><br><span class="line">   <span class="string">'mac'</span> =&gt; <span class="string">'xxx...'</span>   <span class="comment">// 后续请求验证数据合法性的字符串</span></span><br><span class="line">]https:<span class="comment">//blog.baiguiren.com/2018/01/11/laravel/Laravel%20cookie%20%E5%8A%A0%E5%AF%86%E8%A7%A3%E5%AF%86%E5%8E%9F%E7%90%86/</span></span><br></pre></td></tr></table></figure>
<h3 id="Eloquent-Builder-添加自定义方法"><a href="#Eloquent-Builder-添加自定义方法" class="headerlink" title="Eloquent Builder 添加自定义方法"></a>Eloquent Builder 添加自定义方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">\Illuminate\Database\Query\Builder\Builder::macro(<span class="string">'active'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;where(<span class="string">'status'</span>, <span class="number">1</span>);</span><br><span class="line">&#125;);</span><br><span class="line">DB::table(xxx)-&gt;active()-&gt;get();</span><br><span class="line"></span><br><span class="line">\App\Model\User::active()-&gt;first();</span><br><span class="line">在 app/Providers 下面新建一个 Provider，把 macro 调用放到 Provider 的 register 方法中</span><br><span class="line">namespace App\Providers;</span><br><span class="line"> </span><br><span class="line">use Illuminate\Database\Query\Builder;</span><br><span class="line">use Illuminate\Support\ServiceProvider;</span><br><span class="line"> </span><br><span class="line"><span class="comment">/** @mixin \Illuminate\Database\Eloquent\Builder */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DatabaseServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Register any application services.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Builder::macro(<span class="string">'active'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;where(<span class="string">'status'</span>, <span class="number">1</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">加了 Providers 之后还要在 config/app.php 中配置这个 Provider。</span><br></pre></td></tr></table></figure>
<h3 id="修改模型对象的某个属性"><a href="#修改模型对象的某个属性" class="headerlink" title="修改模型对象的某个属性"></a>修改模型对象的某个属性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">$user = <span class="keyword">new</span> User;</span><br><span class="line">$user-&gt;age = <span class="number">26</span>;</span><br><span class="line">dd($user-&gt;toArray(), $user-&gt;getAttributes()); <span class="comment">// ['age' =&gt; 26], ['age' =&gt; 26]</span></span><br><span class="line"></span><br><span class="line">我们想修改里面属性的时候比如 $user-&gt;info[<span class="string">'job'</span>] = <span class="string">'PHP'</span>;，实际上等同于：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">$user-&gt;__get(<span class="string">'info'</span>)[<span class="string">'job'</span>] = <span class="string">'PHP'</span>;</span><br><span class="line">也就是说，等同于：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">$temp = $user-&gt;__get(<span class="string">'info'</span>);</span><br><span class="line">$temp[<span class="string">'job'</span>] = <span class="string">'PHP'</span>;</span><br><span class="line">也就是说，我们在设置有层级的属性的时候，中间产生了临时变量。</span><br><span class="line"></span><br><span class="line">假设大家知道了值传递、引用传递的区别。</span><br><span class="line"></span><br><span class="line">这个时候问题就来了，如果这个 $user-&gt;info 是一个数组，我们最终修改的是一个临时数组，而 $user-&gt;info 还是不变。这个时候就报错了，因为这个修改是完全没有意义的。</span><br><span class="line"></span><br><span class="line">而如果 $user-&gt;info 是一个对象的时候，我们临时变量也是一个对象，而对象是通过引用传递的，我们修改了这个对象的时候，实际上也修改了 $user-&gt;info，因为这个临时变量指向了原始对象。</span><br><span class="line"></span><br><span class="line">$user = app(User::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line">$user-&gt;infoObj = <span class="keyword">new</span> stdClass;</span><br><span class="line">$user-&gt;infoObj-&gt;age = <span class="number">26</span>;</span><br><span class="line">$user-&gt;infoObj-&gt;job = <span class="string">'PHP'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// dd($user-&gt;toArray());</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Indirect modification of overloaded property User::$info has no effect</span></span><br><span class="line">$user-&gt;info = [</span><br><span class="line">    <span class="string">'age'</span> =&gt; <span class="number">26</span>,</span><br><span class="line">];</span><br><span class="line">$user-&gt;info[<span class="string">'job'</span>] = <span class="string">'PHP'</span>;</span><br><span class="line">https:<span class="comment">//blog.baiguiren.com/2018/07/05/laravel/Laravel%20Model%20-%20Indirect%20modification%20of%20overloaded%20property/</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel技巧"><a href="#laravel技巧" class="headerlink" title="laravel技巧"></a>laravel技巧</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">使用一次查询代替多次查询AdminUser::<span class="keyword">with</span>(<span class="string">'admin_user_info'</span>)-&gt;where(<span class="string">'id'</span>, <span class="string">'&gt;'</span>, <span class="number">1</span>)-&gt;get();</span><br><span class="line">$products = \DB::table(<span class="string">'product'</span>)-&gt;where(<span class="string">'category_id'</span>, <span class="number">1</span>)-&gt;paginate();</span><br><span class="line">$product_ids = $products-&gt;pluck(<span class="string">'id'</span>)-&gt;toArray(); <span class="comment">// 获取所有 id , 下面第二点有说到这个用法&lt;br&gt;</span></span><br><span class="line">$some_other_infos = \DB::table(<span class="string">'some_other_info'</span>) <span class="comment">// 根据 id 数组一次查询所有关联数据</span></span><br><span class="line">    -&gt;whereIn(<span class="string">'product_id'</span>, $product_ids)</span><br><span class="line">    -&gt;get(); </span><br><span class="line">$some_other_infos = array_column($some_other_infos, <span class="literal">null</span>, <span class="string">'product_id'</span>); <span class="comment">// 使用 id 把结果集转换为关联数组，这样下面可以更高效地操作，否则我们只能两次 foreach 了</span></span><br><span class="line">foreach ($products <span class="keyword">as</span> $key =&gt; $product) &#123;</span><br><span class="line">    $product[<span class="string">'some_other_info'</span>] = array_get($some_other_infos, $product[<span class="string">'id'</span>]);</span><br><span class="line">    $products[$key] = $product;</span><br><span class="line">&#125;</span><br><span class="line">namespace App\Model;</span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AdminUser</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"> </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">admin_user_info</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasOne(AdminUserInfo::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$adminUsers = AdminUser::limit(<span class="number">10</span>)-&gt;get();</span><br><span class="line">$adminUsers-&gt;load(<span class="string">'admin_user_info'</span>);</span><br><span class="line">array_get、data_get 方法代替多个 isset 判断</span><br><span class="line"><span class="comment">// 获取一个用户下文章的一条评论</span></span><br><span class="line">$user = User::<span class="keyword">with</span>([<span class="string">'article.comment'</span>])-&gt;first();</span><br><span class="line">$content = isset($user-&gt;article) ? (isset($user-&gt;article-&gt;comment) ? $user-&gt;article-&gt;comment : <span class="string">''</span>) : <span class="string">''</span>; <span class="comment">// 传统写法</span></span><br><span class="line">$content = data_get($user, <span class="string">'article.comment.content'</span>); <span class="comment">// 评论内容</span></span><br><span class="line">单数命名时候 Model 类不用定义 protected $table</span><br><span class="line">重写 Model 类的 getTable 方法</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getTable</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;table)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;table;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> str_replace(<span class="string">'\\'</span>, <span class="string">''</span>, <span class="attr">Str</span>::snake(class_basename($<span class="keyword">this</span>)));</span><br><span class="line">&#125;</span><br><span class="line">$validator = \Validator::make(request()-&gt;all(), [</span><br><span class="line">    <span class="string">'buyer_id'</span> =&gt; <span class="string">'required'</span>,</span><br><span class="line">    <span class="string">'amount'</span> =&gt; <span class="string">'required|numeric|gt:0'</span></span><br><span class="line">], [</span><br><span class="line">    <span class="string">'buyer_id.required'</span> =&gt; <span class="string">':attribute不能为空'</span>,</span><br><span class="line">    <span class="string">'amount.required'</span> =&gt; <span class="string">'请填写:attribute'</span>,</span><br><span class="line">    <span class="string">'amount.numeric'</span> =&gt; <span class="string">':attribute必须为数字'</span>,</span><br><span class="line">    <span class="string">'amount.gt'</span> =&gt; <span class="string">':attribute必须大于0'</span></span><br><span class="line">], [</span><br><span class="line">    <span class="string">'buyer_id'</span> =&gt; <span class="string">'客户id'</span>,</span><br><span class="line">    <span class="string">'amount'</span> =&gt; <span class="string">'金额'</span></span><br><span class="line">]);</span><br><span class="line"><span class="keyword">if</span> ($validator-&gt;fails()) &#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;ajaxFail($validator-&gt;messages()-&gt;first());</span><br><span class="line">&#125; https:<span class="comment">//blog.baiguiren.com/2018/07/29/laravel/%E4%B8%AA%E4%BA%BA%E5%9C%A8%20Laravel%20%E5%BC%80%E5%8F%91%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E6%8A%80%E5%B7%A7(%E6%8C%81%E7%BB%AD%E6%9B%B4%E6%96%B0)/</span></span><br></pre></td></tr></table></figure>
<h3 id="外部调用类的私有方法"><a href="#外部调用类的私有方法" class="headerlink" title="外部调用类的私有方法"></a>外部调用类的私有方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $protected = <span class="string">'protected'</span>;</span><br><span class="line"></span><br><span class="line">    private $private = <span class="string">'private'</span>;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">protectedMethod</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'Hello protected'</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">privateMethod</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'Hello private'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__get</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;$name;</span><br><span class="line">            &#125;</span><br><span class="line">    </span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$name, $arguments</span>)</span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;$name(...$<span class="built_in">arguments</span>);</span><br><span class="line">            &#125;</span><br><span class="line">&#125;</span><br><span class="line">$test = <span class="keyword">new</span> A();</span><br><span class="line"></span><br><span class="line">$test-&gt;protected;</span><br><span class="line">$test-&gt;private;</span><br><span class="line">$test-&gt;protectedMethod();</span><br><span class="line">$test-&gt;privateMethod();</span><br><span class="line">$test = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo $<span class="keyword">this</span>-&gt;private;</span><br><span class="line">    echo $<span class="keyword">this</span>-&gt;privateMethod();</span><br><span class="line">&#125;;</span><br><span class="line">$test = $test-&gt;bindTo(<span class="keyword">new</span> A(), <span class="string">'A'</span>);</span><br><span class="line">$test();</span><br></pre></td></tr></table></figure>
<h3 id="30-分钟未付款自动取消订单"><a href="#30-分钟未付款自动取消订单" class="headerlink" title="30 分钟未付款自动取消订单"></a>30 分钟未付款自动取消订单</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">先保证 redis 的版本大于 <span class="number">2.8</span> ，现在绝大部分不成问题了，然后修改 redis 的配置文件，加入：notify-keyspace-events <span class="string">"Ex"</span></span><br><span class="line">Cache::store(<span class="string">'redis'</span>)-&gt;put(<span class="string">'ORDER_CONFIRM:'</span>.$order-&gt;id,$order-&gt;id,<span class="number">30</span>); <span class="comment">// 30分钟后过期--执行取消订单</span></span><br><span class="line"> 然后我们来监听 ORDER_CONFIRM:ORDER_ID 的过期事件</span><br><span class="line">先建个命令，我们一会儿的监听全靠他了。</span><br><span class="line">php artisan make:command OrderExpireListen</span><br><span class="line">  然后把命令执行文件 app\Console\Commands\OrderExpireListen.php 写成这样:</span><br><span class="line">namespace App\Console\Commands;</span><br><span class="line">use App\Http\Models\Order;</span><br><span class="line">use Illuminate\Console\Command;</span><br><span class="line">use Illuminate\Support\Facades\Redis <span class="keyword">as</span> Redis;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OrderExpireListen</span> <span class="keyword">extends</span> <span class="title">Command</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The name and signature of the console command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $signature = <span class="string">'order:expire'</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The console command description.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $description = <span class="string">'监听订单创建，在30分钟后如果没付款取消订单。'</span>;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Create a new command instance.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::__construct();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute the console command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">        $cachedb = config(<span class="string">'database.redis.cache.database'</span>,<span class="number">0</span>);</span><br><span class="line">        $pattern = <span class="string">'__keyevent@'</span>.$cachedb.<span class="string">'__:expired'</span>;</span><br><span class="line">        Redis::subscribe($pattern,<span class="function"><span class="keyword">function</span> (<span class="params">$channel</span>)</span>&#123;     <span class="comment">// 订阅键过期事件</span></span><br><span class="line">            $key_type = str_before($channel,<span class="string">':'</span>);</span><br><span class="line">            <span class="keyword">switch</span> ($key_type) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">'ORDER_CONFIRM'</span>:</span><br><span class="line">                    $order_id = str_after($channel,<span class="string">':'</span>);    <span class="comment">// 取出订单 ID</span></span><br><span class="line">                    $order = Order::find($order_id);</span><br><span class="line">                    <span class="keyword">if</span> ($order) &#123;</span><br><span class="line">                        $order-&gt;cancel(); <span class="comment">// 执行取消操作</span></span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">'ORDER_OTHEREVENT'</span>:</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">default</span>:</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> 文件好了之后，使用</span><br><span class="line">php artisan order:expire</span><br><span class="line"> </span><br><span class="line">解决自动断开连接超时现象</span><br><span class="line">你改下 app\config\database.php 中 redis 节，增加一个 read_write_timeout :</span><br><span class="line"> <span class="string">'redis'</span> =&gt; [</span><br><span class="line">        <span class="string">'client'</span> =&gt; <span class="string">'predis'</span>,</span><br><span class="line">        <span class="string">'default'</span> =&gt; [</span><br><span class="line">            <span class="string">'host'</span> =&gt; env(<span class="string">'REDIS_HOST'</span>, <span class="string">'127.0.0.1'</span>),</span><br><span class="line">            <span class="string">'password'</span> =&gt; env(<span class="string">'REDIS_PASSWORD'</span>, <span class="literal">null</span>),</span><br><span class="line">            <span class="string">'port'</span> =&gt; env(<span class="string">'REDIS_PORT'</span>, <span class="number">6379</span>),</span><br><span class="line">            <span class="string">'database'</span> =&gt; env(<span class="string">'REDIS_DB'</span>, <span class="number">0</span>),</span><br><span class="line">            <span class="string">'read_write_timeout'</span> =&gt; env(<span class="string">'REDIS_RW_TIMEOUT'</span>, <span class="number">5</span>),  <span class="comment">// 读写超时设定</span></span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line">最后：使用supervisor监听order:expire  http:<span class="comment">//118.25.60.91:9000/articles/30-fen-zhong-wei-fu-kuan-zi-dong-qu-xiao-ding-dan</span></span><br><span class="line">https:<span class="comment">//crazyfzw.github.io/2019/03/26/redis-keyspace-notifications/ </span></span><br><span class="line">http:<span class="comment">//redisdoc.com/topic/notification.html</span></span><br></pre></td></tr></table></figure>
<h3 id="guzzle异步请求"><a href="#guzzle异步请求" class="headerlink" title="guzzle异步请求"></a>guzzle异步请求</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">$request = <span class="keyword">new</span> \GuzzleHttp\Psr7\Request(<span class="string">'GET'</span>, <span class="string">'http://www.example.com'</span>);</span><br><span class="line">$promise = $client-&gt;sendAsync($request)-&gt;then(<span class="function"><span class="keyword">function</span> (<span class="params">$response</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 请求完成后的操作</span></span><br><span class="line">  $response-&gt;getBody();</span><br><span class="line">&#125;);</span><br><span class="line">$promise-&gt;wait();</span><br><span class="line"></span><br><span class="line">$client = <span class="keyword">new</span> Client([<span class="string">'base_uri'</span> =&gt; <span class="string">'http://httpbin.org/ip'</span>]);</span><br><span class="line">        $promise = $client-&gt;requestAsync(<span class="string">'GET'</span>, <span class="string">'http://httpbin.org/ip'</span>);</span><br><span class="line">        $promise-&gt;then(</span><br><span class="line">            <span class="function"><span class="keyword">function</span> (<span class="params">ResponseInterface $res</span>) </span>&#123;</span><br><span class="line">                echo $res-&gt;getStatusCode() . <span class="string">"\n"</span>;</span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="function"><span class="keyword">function</span> (<span class="params">RequestException $e</span>) </span>&#123;</span><br><span class="line">                echo $e-&gt;getMessage() . <span class="string">"\n"</span>;</span><br><span class="line">                echo $e-&gt;getRequest()-&gt;getMethod();</span><br><span class="line">            &#125;</span><br><span class="line">        );</span><br><span class="line">        dd($promise);</span><br><span class="line"> <span class="string">"guzzlehttp/guzzle"</span>: <span class="string">"5.3.1"</span>,  </span><br><span class="line"> $client=<span class="keyword">new</span> GuzzleHttp\Client();</span><br><span class="line">         $reqs = $client-&gt;createRequest(<span class="string">'GET'</span>, <span class="string">'http://httpbin.org/ip'</span>, [<span class="string">'future'</span> =&gt; <span class="literal">true</span>]);</span><br><span class="line">         $client-&gt;send($reqs)-&gt;then(<span class="function"><span class="keyword">function</span> (<span class="params">$response</span>)</span>&#123;</span><br><span class="line">             dump($response-&gt;json());<span class="comment">//["origin" =&gt; "49.7.40.196, 49.7.40.196"] 这个输出在ok之后</span></span><br><span class="line">         &#125;);</span><br><span class="line">        </span><br><span class="line">         dump(<span class="string">'ok'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="删除数组元素"><a href="#删除数组元素" class="headerlink" title="删除数组元素"></a>删除数组元素</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">删除数组元素有两种方式，第一种是使用 unset 方法。</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">unset($arr[<span class="number">1</span>]);</span><br><span class="line">此方法删除元素后，$arr[<span class="number">1</span>] 为 <span class="literal">null</span> ，所有元素的索引值不变，也就是说元素不会重新排列，所以不是很实用。</span><br><span class="line"></span><br><span class="line">要使元素删除后重新排列顺序，使用 array_splice 方法即可。</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">array_splice(array,start,length,array);</span><br><span class="line">array_splice 的四个参数中，array 为要删除元素的数组；start 为要开始删除元素的位置（索引值）；length 为要删除的元素数量；最后一个参数为可选参数，允许用一个新元素替代被删除的元素。</span><br><span class="line"></span><br><span class="line">使用 array_splice 删除数组中索引为<span class="number">1</span>的元素：</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">array_splice($arr,<span class="number">1</span>,<span class="number">1</span>);<span class="comment">//重新索引了 不用array_values</span></span><br></pre></td></tr></table></figure>
<h3 id="如何查Session内容"><a href="#如何查Session内容" class="headerlink" title="如何查Session内容"></a>如何查Session内容</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">首先配置Session存储，使用Memcached</span><br><span class="line"></span><br><span class="line">session.save_handler = memcached </span><br><span class="line">session.save_path = <span class="string">"localhost:11211"</span> </span><br><span class="line">查看session_id</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">session_start();</span><br><span class="line">var_dump(session_id());</span><br><span class="line">我们希望在Memcached里查询Session内容，怎么确定查询的Key呢？</span><br><span class="line"></span><br><span class="line">尝试：</span><br><span class="line"></span><br><span class="line">get vkfc4eefu7ihlu5iv7v3je8rm0 </span><br><span class="line">无效，应该这样:</span><br><span class="line"></span><br><span class="line">get memc.sess.key.vkfc4eefu7ihlu5iv7v3je8rm0</span><br><span class="line">原来在使用Memcached存储Session, key前缀是可以配置的。</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">var_dump(ini_get(<span class="string">'memcached.sess_prefix'</span>));  <span class="comment">// 'memc.sess.key.'</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//php1024.com/posts/50.htm</span></span><br></pre></td></tr></table></figure>
<h3 id="redis队列缓存"><a href="#redis队列缓存" class="headerlink" title="redis队列缓存"></a>redis队列缓存</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 加上时间戳存入队列</span></span><br><span class="line">$now_time = date(<span class="string">"Y-m-d H:i:s"</span>);</span><br><span class="line">$redis-&gt;rPush(<span class="string">"call_log"</span>, $_GET[<span class="string">'info'</span>] . <span class="string">"%"</span> . $now_time);</span><br><span class="line">$redis-&gt;close();</span><br><span class="line"></span><br><span class="line"><span class="comment">// init redis</span></span><br><span class="line">$redis_xx = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis_xx-&gt;connect(<span class="string">'ip'</span>, port);</span><br><span class="line">$redis_xx-&gt;auth(<span class="string">"password"</span>);</span><br><span class="line"><span class="comment">// 获取现有消息队列的长度</span></span><br><span class="line">$count = <span class="number">0</span>;</span><br><span class="line">$max = $redis_xx-&gt;lLen(<span class="string">"call_log"</span>);</span><br><span class="line"><span class="comment">// 获取消息队列的内容，拼接sql</span></span><br><span class="line">$insert_sql = <span class="string">"insert into fb_call_log (`interface_name`, `createtime`) values "</span>;</span><br><span class="line"><span class="comment">// 回滚数组</span></span><br><span class="line">$roll_back_arr = array();</span><br><span class="line"><span class="keyword">while</span> ($count &lt; $max) &#123;</span><br><span class="line">    $log_info = $redis_cq01-&gt;lPop(<span class="string">"call_log"</span>);</span><br><span class="line">    $roll_back_arr = $log_info;</span><br><span class="line">    <span class="keyword">if</span> ($log_info == <span class="string">'nil'</span> || !isset($log_info)) &#123;</span><br><span class="line">        $insert_sql .= <span class="string">";"</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 切割出时间和info</span></span><br><span class="line">    $log_info_arr = explode(<span class="string">"%"</span>,$log_info);</span><br><span class="line">    $insert_sql .= <span class="string">" ('"</span>.$log_info_arr[<span class="number">0</span>].<span class="string">"','"</span>.$log_info_arr[<span class="number">1</span>].<span class="string">"'),"</span>;</span><br><span class="line">    $count++;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 判定存在数据，批量入库</span></span><br><span class="line"><span class="keyword">if</span> ($count != <span class="number">0</span>) &#123;</span><br><span class="line">    $link_2004 = mysql_connect(<span class="string">'ip:port'</span>, <span class="string">'user'</span>, <span class="string">'password'</span>);</span><br><span class="line">    <span class="keyword">if</span> (!$link_2004) &#123;</span><br><span class="line">        die(<span class="string">"Could not connect:"</span> . mysql_error());</span><br><span class="line">    &#125;</span><br><span class="line">    $crowd_db = mysql_select_db(<span class="string">'fb_log'</span>, $link_2004);</span><br><span class="line">    $insert_sql = rtrim($insert_sql,<span class="string">","</span>).<span class="string">";"</span>;</span><br><span class="line">    $res = mysql_query($insert_sql);</span><br><span class="line">    <span class="comment">// 输出入库log和入库结果;</span></span><br><span class="line">    echo date(<span class="string">"Y-m-d H:i:s"</span>).<span class="string">"insert "</span>.$count.<span class="string">" log info result:"</span>;</span><br><span class="line">    echo json_encode($res);</span><br><span class="line">    echo <span class="string">"&lt;/br&gt;\n"</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 数据库插入失败回滚</span></span><br><span class="line">    <span class="keyword">if</span>(!$res)&#123;</span><br><span class="line">       foreach($roll_back_arr <span class="keyword">as</span> $k)&#123;</span><br><span class="line">           $redis_xx-&gt;rPush(<span class="string">"call_log"</span>, $k);</span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// 释放连接</span></span><br><span class="line">    mysql_free_result($res);</span><br><span class="line">    mysql_close($link_2004);</span><br><span class="line">&#125;</span><br><span class="line">http:<span class="comment">//cuihuan.net/2017/01/20/%E3%80%90%E9%AB%98%E5%B9%B6%E5%8F%91%E7%AE%80%E5%8D%95%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E3%80%91redis%E9%98%9F%E5%88%97%E7%BC%93%E5%AD%98%20+%20mysql%20%E6%89%B9%E9%87%8F%E5%85%A5%E5%BA%93%20+%20php%E7%A6%BB%E7%BA%BF%E6%95%B4%E5%90%88/</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel服务容器"><a href="#laravel服务容器" class="headerlink" title="laravel服务容器"></a>laravel服务容器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserStatsCsvExporter</span> <span class="title">implements</span> <span class="title">UserStatsExporterContract</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">export</span>(<span class="params">int $userId</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">         <span class="comment">// Load user statistics...</span></span><br><span class="line">         <span class="comment">// Export file...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在控制器中，我们会 <span class="keyword">new</span> 一个类，然后调用里面的 <span class="keyword">export</span> 方法。</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExportController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $userStatsExporter = <span class="keyword">new</span> UserStatsCsvExporter();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $userStatsExporter-&gt;<span class="keyword">export</span>(<span class="number">12</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle2</span>(<span class="params">UserStatsCsvExporter $userStatsExporter</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $userStatsExporter-&gt;<span class="keyword">export</span>(<span class="number">12</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserStatsCsvExporter</span> <span class="title">implements</span> <span class="title">UserStatsExporterContract</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** @var Translator */</span></span><br><span class="line">    private $translator;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Translator $translator</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;translator = $translator;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">export</span>(<span class="params">int $userId</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// Load user statistics...</span></span><br><span class="line">        <span class="comment">// Export file...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Translator</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/** @var string */</span></span><br><span class="line">    private $language;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">string $language</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;language = $language;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">translate</span>(<span class="params">string $word</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// Translate word...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserStatsExporterProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;app-&gt;bind(UserStatsCsvExporter::<span class="class"><span class="keyword">class</span>, <span class="title">function</span>() </span>&#123;</span><br><span class="line">           <span class="keyword">return</span> <span class="keyword">new</span> UserStatsCsvExporter(<span class="keyword">new</span> Translator(config(<span class="string">'app.locale'</span>)));</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">绑定到接口</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">UserStatsXmlExporter $userStatsExporter</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $userStatsExporter-&gt;<span class="keyword">export</span>(<span class="number">12</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;app-&gt;bind(UserStatsXmlExporter::<span class="class"><span class="keyword">class</span>, <span class="title">function</span>() </span>&#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> UserStatsXmlExporter(<span class="keyword">new</span> Translator(config(<span class="string">'app.locale'</span>))</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">UserStatsExporterContract $userStatsExporter</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $userStatsExporter-&gt;<span class="keyword">export</span>(<span class="number">12</span>);</span><br><span class="line">&#125;</span><br><span class="line">要让上面代码工作，我们还需要改动服务提供者的代码。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;app-&gt;bind(UserStatsExporterContract::<span class="class"><span class="keyword">class</span>, <span class="title">function</span>() </span>&#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> UserStatsXmlExporter(<span class="keyword">new</span> Translator(config(<span class="string">'app.locale'</span>)));</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">UserStatsExporterContract $userStatsExporter</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    dd(app(UserStatsExporterContract::class), app(UserStatsExporterContract::class));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $userStatsExporter-&gt;<span class="keyword">export</span>(<span class="number">12</span>);</span><br><span class="line">&#125;</span><br><span class="line">使用 singleton 来替换 bind 方法即可。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;app-&gt;singleton(UserStatsExporterContract::<span class="class"><span class="keyword">class</span>, <span class="title">function</span>() </span>&#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="keyword">new</span> UserStatsXmlExporter(<span class="keyword">new</span> Translator(config(<span class="string">'app.locale'</span>)));</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32333</span></span><br></pre></td></tr></table></figure>
<h3 id="反射动态代理"><a href="#反射动态代理" class="headerlink" title="反射动态代理"></a>反射动态代理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">mysql</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params">$db</span>)</span>&#123;</span><br><span class="line">        echo <span class="string">"连接到数据库$&#123;db[0]&#125;\r\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">sqlproxy</span></span>&#123;</span><br><span class="line">    private $target;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$tar</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;target[]=<span class="keyword">new</span> $tar();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$name, $args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;target <span class="keyword">as</span> $obj) &#123;</span><br><span class="line">            $r=<span class="keyword">new</span> ReflectionClass($obj);</span><br><span class="line">            <span class="keyword">if</span> ($method= $r-&gt;getMethod($name)) &#123;</span><br><span class="line">                <span class="keyword">if</span> ($method-&gt;isPublic() &amp;&amp; !$method-&gt;isAbstract()) &#123;</span><br><span class="line">                    echo <span class="string">"方法前拦截记录LOG\r\n"</span>;</span><br><span class="line">                    $method-&gt;invoke($obj,$args);</span><br><span class="line">                    echo <span class="string">"方法后拦截\r\n"</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$obj=<span class="keyword">new</span> sqlproxy(<span class="string">'mysql'</span>);</span><br><span class="line">$obj-&gt;connect(<span class="string">'member'</span>);</span><br><span class="line">方法前拦截记录LOG</span><br><span class="line">连接到数据库member</span><br><span class="line">方法后拦截</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32943</span></span><br></pre></td></tr></table></figure>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//pecl.php.net/package/imagick  </span></span><br><span class="line">#wget http://pecl.php.net/get/imagick-3.4.4.tgz</span><br><span class="line">#tar -xzvf imagick-3.4.4.tgz</span><br><span class="line">#cd imagick-3.4.4</span><br><span class="line">#phpize</span><br><span class="line">#./configure --with-php-config=/php7/bin/php-config --with-imagick=/usr/local/imagemagick</span><br><span class="line">checking Testing /usr/bin/Wand-config... Doesn<span class="string">'t exist</span></span><br><span class="line"><span class="string">checking Testing /usr/sbin/bin/MagickWand-config... Doesn'</span>t exist</span><br><span class="line">checking Testing /usr/sbin/bin/Wand-config... Doesn<span class="string">'t exist</span></span><br><span class="line"><span class="string">checking Testing /opt/bin/MagickWand-config... Doesn'</span>t exist</span><br><span class="line">checking Testing /opt/bin/Wand-config... Doesn<span class="string">'t exist</span></span><br><span class="line"><span class="string">checking Testing /opt/local/bin/MagickWand-config... Doesn'</span>t exist</span><br><span class="line">checking Testing /opt/local/bin/Wand-config... Doesn<span class="string">'t exist</span></span><br><span class="line"><span class="string">configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> #yum install ImageMagick-devel -y </span></span><br><span class="line"><span class="string"># ./configure --with-php-config=/php7/bin/php-config --with-imagick=/usr/local/imagemagick</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> checking whether stripping libraries is possible... yes</span></span><br><span class="line"><span class="string"> checking if libtool supports shared libraries... yes</span></span><br><span class="line"><span class="string"> checking whether to build shared libraries... yes</span></span><br><span class="line"><span class="string"> checking whether to build static libraries... no</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> creating libtool</span></span><br><span class="line"><span class="string"> appending configuration tag "CXX" to libtool</span></span><br><span class="line"><span class="string"> configure: creating ./config.status</span></span><br><span class="line"><span class="string"> config.status: creating config.h</span></span><br><span class="line"><span class="string">#make &amp;&amp; make install </span></span><br><span class="line"><span class="string">Build complete.</span></span><br><span class="line"><span class="string">Don'</span>t forget to run <span class="string">'make test'</span>.</span><br><span class="line"></span><br><span class="line">Installing shared extensions:     <span class="regexp">/php7/</span>lib/php/extensions/no-debug-zts<span class="number">-20151012</span>/</span><br><span class="line">Installing header files:          <span class="regexp">/php7/i</span>nclude/php/</span><br><span class="line">php7 --ini</span><br><span class="line">Configuration File (php.ini) Path: <span class="regexp">/opt/</span>php7/etc/</span><br><span class="line">Loaded Configuration File: <span class="regexp">/opt/</span>php7/etc/php.ini</span><br><span class="line">Scan <span class="keyword">for</span> additional .ini files <span class="keyword">in</span>: (none)</span><br><span class="line">Additional .ini files parsed: (none)</span><br><span class="line"> #vi php.ini</span><br><span class="line"> extension=<span class="regexp">/opt/</span>php7/lib/php/extensions/no-debug-zts<span class="number">-20151012</span>/imagick.so</span><br></pre></td></tr></table></figure>
<h3 id="phpstorm-gitbash"><a href="#phpstorm-gitbash" class="headerlink" title="phpstorm gitbash"></a>phpstorm gitbash</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">打开 PHPStorm 的设置，定位到 Tools -&gt; Terminal</span><br><span class="line">将 Shell Path 选项改为 &#123;git 安装目录&#125;\bin\sh.exe，重新打开终端，可以看到已切换成了 git bash</span><br><span class="line">另外 git bash 可能会出现中文乱码的问题，需要在 Git 的安装目录下找到 ./etc/bash.bashrc 文件，在末尾添加：</span><br><span class="line"></span><br><span class="line">$ vim ./etc/bash.bashrc</span><br><span class="line"></span><br><span class="line"># support chinese</span><br><span class="line"><span class="keyword">export</span> LANG=<span class="string">"zh_CN.UTF-8"</span></span><br><span class="line"><span class="keyword">export</span> LC_ALL=<span class="string">"zh_CN.UTF-8"</span></span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/32981</span></span><br></pre></td></tr></table></figure>
<h3 id="composer-找不到文件"><a href="#composer-找不到文件" class="headerlink" title="composer 找不到文件"></a>composer 找不到文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> findClasses 函数中，有这么一个去除代码中 heredoc 或 nowdoc 的操作：</span><br><span class="line"></span><br><span class="line"><span class="comment">// strip heredocs/nowdocs</span></span><br><span class="line">$contents = preg_replace(<span class="string">'&#123;&lt;&lt;&lt;[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])&#125;s'</span>, <span class="string">'null'</span>, $contents);</span><br><span class="line">问题就出在这个正则。对于内容比较长的类源文件，这个正则替换可能会直接报错返回 <span class="literal">null</span>，这样就导致 composer 无法识别出该类，从而你的应用中就会报类不存在的错误。</span><br><span class="line"></span><br><span class="line">通过调用 preg_last_error () 函数可以获取正则错误代码。我这里返回的错误代码为 <span class="number">2</span>，也就是 PREG_BACKTRACK_LIMIT_ERROR，意思是回溯限制错误。这个错误受 php 配置影响，可以把 pcre.backtrack_limit 参数设置更大或者直接设置成 - <span class="number">1</span> 不受限制（可能会造成性能问题，谨慎操作）。这也是前面提到的为什么有的机器上正常，有的机器上又不正常，原因就在于这个配置不同。</span><br><span class="line"></span><br><span class="line">OK，到此弄清楚是什么原因了。解决办法：<span class="number">1</span>、可以将 pcre.backtrack_limit 参数设置大点再试试 <span class="number">2</span>、看是否能减小类源文件中 heredocs/nowdocs 字符串的大小。此处我的解决办法是直接把这个超长 heredocs 字符串独立到 sql 文件中，代码中读取文件内容即可。</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//learnku.com/articles/32972</span></span><br></pre></td></tr></table></figure>
<h3 id="分组查询"><a href="#分组查询" class="headerlink" title="分组查询"></a>分组查询</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">select <span class="string">`u`</span>.<span class="string">`id`</span>,<span class="string">`u`</span>.<span class="string">`name`</span>,<span class="string">`u`</span>.<span class="string">`num`</span> <span class="keyword">from</span> <span class="string">`users`</span> <span class="keyword">as</span> <span class="string">`u`</span> left join (select <span class="string">`user_id`</span>,count(*) <span class="keyword">as</span> <span class="string">`num`</span> <span class="keyword">from</span> books group by <span class="string">`user_id`</span>) <span class="keyword">as</span> <span class="string">`b`</span> on <span class="string">`u`</span>.id = <span class="string">`b`</span>.user_id  查询每个人的书数量</span><br><span class="line">select distinct <span class="string">`u`</span>.<span class="string">`id`</span>,<span class="string">`u`</span>.<span class="string">`name`</span>,IFNULL( <span class="string">`b`</span>.<span class="string">`num`</span>, <span class="number">0</span> ) AS num <span class="keyword">from</span> <span class="string">`users`</span> <span class="keyword">as</span> <span class="string">`u`</span> left join (select <span class="string">`user_id`</span>,count(*) <span class="keyword">as</span> <span class="string">`num`</span> <span class="keyword">from</span> books group by <span class="string">`user_id`</span>) <span class="keyword">as</span> <span class="string">`b`</span> on <span class="string">`u`</span>.id = <span class="string">`b`</span>.user_id</span><br><span class="line"></span><br><span class="line"> <span class="comment">//统计出所有内部员工的user_id</span></span><br><span class="line"></span><br><span class="line">    $user_ids = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line"></span><br><span class="line">    $bookQuery = Book::selectRaw(<span class="string">'user_id,count(*) as number'</span>)-&gt;whereIn(<span class="string">'user_id'</span>, $user_ids)-&gt;groupBy(<span class="string">'user_id'</span>); <span class="comment">//制作一个query builder</span></span><br><span class="line"></span><br><span class="line">    $users = User::selectRaw(<span class="string">'u.id,IFNULL( b.number, 0 ) AS number'</span>)</span><br><span class="line">        -&gt;<span class="keyword">from</span>(<span class="string">'users as u'</span>)</span><br><span class="line">        -&gt;distinct()</span><br><span class="line">        -&gt;whereIn(<span class="string">'id'</span>, $user_ids)</span><br><span class="line">        -&gt;leftJoin(\DB::raw(<span class="string">"(&#123;$bookQuery-&gt;toSql()&#125;) as b"</span>),<span class="function"><span class="keyword">function</span> (<span class="params">$join</span>) <span class="title">use</span>(<span class="params">$bookQuery</span>)</span>&#123;</span><br><span class="line">            <span class="comment">//toSql()返回的是等待绑定参数的SQL语句</span></span><br><span class="line">            $join-&gt;mergeBindings($bookQuery-&gt;getQuery())-&gt;on(<span class="string">'u.id'</span>,<span class="string">'='</span>,<span class="string">'b.user_id'</span>);</span><br><span class="line">            <span class="comment">//mergeBindings是将SQl的参数进行绑定</span></span><br><span class="line">        &#125;)</span><br><span class="line">        -&gt;get();</span><br><span class="line">https:<span class="comment">//www.guaosi.com/2019/03/19/laravel-with-limit-and-group-show-zero/</span></span><br></pre></td></tr></table></figure>
<h3 id="取k个不重复的数"><a href="#取k个不重复的数" class="headerlink" title="取k个不重复的数"></a>取k个不重复的数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRandomN</span>(<span class="params">$max, $num</span>) </span>&#123;</span><br><span class="line">    $count = <span class="number">0</span>;</span><br><span class="line">    $<span class="keyword">return</span> = array();</span><br><span class="line">    <span class="keyword">while</span> ($count &lt; $num) &#123;</span><br><span class="line">        $<span class="keyword">return</span>[] = mt_rand(<span class="number">0</span>, $max);</span><br><span class="line">        $<span class="keyword">return</span> = array_flip(array_flip($<span class="keyword">return</span>));</span><br><span class="line">        $count = count($<span class="keyword">return</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    shuffle($<span class="keyword">return</span>);</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> getRandomN(<span class="number">20</span>, <span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// output:[7,14,6,12,3,4,15,0,16,10]</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getRandomN</span>(<span class="params">$num, $n</span>)</span>&#123;</span><br><span class="line">    $startArray = range(<span class="number">0</span>, $num);</span><br><span class="line">    $resultArray = [];</span><br><span class="line">    <span class="keyword">for</span>($i = <span class="number">0</span>; $i &lt; $n; $i++)</span><br><span class="line">    &#123;</span><br><span class="line">        $random = mt_rand(<span class="number">0</span>, $num - $i);</span><br><span class="line">        $resultArray[$i] = $startArray[$random];</span><br><span class="line">        $startArray[$random] = $startArray[$num - $i - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $resultArray;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> getRandomN(<span class="number">20</span>, <span class="number">10</span>);</span><br><span class="line">https:<span class="comment">//alpha2016.github.io/2019/04/09/PHP%E4%BB%8E0-n%E4%B9%8B%E9%97%B4%E9%9A%8F%E6%9C%BA%E5%8F%96k%E4%B8%AA%E4%B8%8D%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0/</span></span><br></pre></td></tr></table></figure>
<h3 id="Intervention-Image-库处理图片"><a href="#Intervention-Image-库处理图片" class="headerlink" title="Intervention Image 库处理图片"></a>Intervention Image 库处理图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> intervention/image</span><br><span class="line"><span class="comment">// 包含autoload文件</span></span><br><span class="line"><span class="built_in">require</span> <span class="string">'vendor/autoload.php'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 导入ImageManager类</span></span><br><span class="line">use Intervention\Image\ImageManager;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建ImageManager实例并指定要使用的驱动(默认GD库)</span></span><br><span class="line">$manager = <span class="keyword">new</span> ImageManager(array(<span class="string">'driver'</span> =&gt; <span class="string">'imagick'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建Image实例然后操作图片</span></span><br><span class="line">$image = $manager-&gt;make(<span class="string">'public/foo.jpg'</span>)-&gt;resize(<span class="number">300</span>, <span class="number">200</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建一个Image对象</span></span><br><span class="line">$img = Image::make(<span class="string">'public/foo.jpg'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取图片文件大小</span></span><br><span class="line">$size = $img-&gt;filesize();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取图片尺寸（像素）</span></span><br><span class="line">$width  = $img-&gt;width();</span><br><span class="line">$height = $img-&gt;height();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调整图片尺寸</span></span><br><span class="line">$img-&gt;resize(<span class="number">300</span>, <span class="number">200</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调整图片的宽或高</span></span><br><span class="line">$img-&gt;resize(<span class="number">300</span>, <span class="literal">null</span>);</span><br><span class="line">$img-&gt;resize(<span class="literal">null</span>, <span class="number">200</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调整图片的宽同时保持图片比例</span></span><br><span class="line">$img-&gt;resize(<span class="number">300</span>, <span class="literal">null</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$constraint</span>) </span>&#123;</span><br><span class="line">    $constraint-&gt;aspectRatio();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调整图片的高同时保持图片比例</span></span><br><span class="line">$img-&gt;resize(<span class="literal">null</span>, <span class="number">200</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$constraint</span>) </span>&#123;</span><br><span class="line">    $constraint-&gt;aspectRatio();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 避免处理时造成文件大小增加</span></span><br><span class="line">$img-&gt;resize(<span class="literal">null</span>, <span class="number">400</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$constraint</span>) </span>&#123;</span><br><span class="line">    $constraint-&gt;aspectRatio();</span><br><span class="line">    $constraint-&gt;upsize();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 转码为其他格式并压缩图片</span></span><br><span class="line">$img-&gt;encode(<span class="string">'jpg'</span>, <span class="number">75</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 转码为 Data URL</span></span><br><span class="line">$img-&gt;encode(<span class="string">'data-url'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 裁剪图片</span></span><br><span class="line">$img-&gt;crop(<span class="number">100</span>, <span class="number">100</span>, <span class="number">25</span>, <span class="number">25</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 旋转-45度</span></span><br><span class="line">$img-&gt;rotate(<span class="number">-45</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 垂直翻转及水平翻转</span></span><br><span class="line">$img-&gt;flip(<span class="string">'v'</span>);</span><br><span class="line">$img-&gt;flip(<span class="string">'h'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 以最优方案裁剪为600*360</span></span><br><span class="line">$img-&gt;fit(<span class="number">600</span>, <span class="number">360</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 以最优方案裁剪为200*200</span></span><br><span class="line">$img-&gt;fit(<span class="number">200</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 以默认质量保存图片</span></span><br><span class="line">$img-&gt;save(<span class="string">'public/bar.jpg'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 以给定质量保存图片</span></span><br><span class="line">$img-&gt;save(<span class="string">'public/bar.png'</span>, <span class="number">60</span>);</span><br><span class="line">https:<span class="comment">//yiming.blog/article/201702121305.html</span></span><br></pre></td></tr></table></figure>
<h3 id="Redis-PubSub实现简单消息推送"><a href="#Redis-PubSub实现简单消息推送" class="headerlink" title="Redis PubSub实现简单消息推送"></a>Redis PubSub实现简单消息推送</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">      &lt;title&gt;swoole chat room&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">      &lt;meta charset="UTF-8"&gt;</span></span><br><span class="line"><span class="regexp">      &lt;script type="text/</span>javascript<span class="string">"&gt;</span></span><br><span class="line"><span class="string">          if(window.WebSocket)&#123;</span></span><br><span class="line"><span class="string">              // 端口和ip地址对应不要写错</span></span><br><span class="line"><span class="string">              var webSocket = new WebSocket("</span>ws:<span class="comment">//127.0.0.1:9502");</span></span><br><span class="line">              webSocket.onopen = <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span><br><span class="line">                  <span class="comment">//webSocket.send("Hello,WebSocket!"); </span></span><br><span class="line">              &#125;;</span><br><span class="line">              webSocket.onmessage = <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>&#123;</span><br><span class="line">                <span class="keyword">var</span> content = <span class="built_in">document</span>.getElementById(<span class="string">'content'</span>);</span><br><span class="line">                content.innerHTML = content.innerHTML.concat(<span class="string">'&lt;p style="margin-left:20px;height:20px;line-height:20px;"&gt;'</span>+event.data+<span class="string">'&lt;/p&gt;'</span>);</span><br><span class="line">              &#125;</span><br><span class="line">              </span><br><span class="line">              <span class="keyword">var</span> sendMessage = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                  <span class="keyword">var</span> data = <span class="built_in">document</span>.getElementById(<span class="string">'message'</span>).value;</span><br><span class="line">                  webSocket.send(data);</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">              <span class="built_in">console</span>.log(<span class="string">"您的浏览器不支持WebSocket"</span>);</span><br><span class="line">          &#125;</span><br><span class="line">      &lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div style=<span class="string">"width:600px;margin:0 auto;border:1px solid #ccc;"</span>&gt;</span><br><span class="line">        &lt;div id=<span class="string">"content"</span> style=<span class="string">"overflow-y:auto;height:300px;"</span>&gt;&lt;/div&gt;</span><br><span class="line">        &lt;hr/&gt;</span><br><span class="line">        &lt;div style=<span class="string">"height:40px"</span>&gt;</span><br><span class="line">            &lt;input type=<span class="string">"text"</span> id=<span class="string">"message"</span> style=<span class="string">"margin-left:10px;height:25px;width:450px;"</span>&gt;</span><br><span class="line">            &lt;button onclick=<span class="string">"sendMessage()"</span> style=<span class="string">"height:28px;width:75px;"</span>&gt;发送&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">        &lt;/</span>div&gt;</span><br><span class="line">    &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>body&gt;</span><br><span class="line">&lt;<span class="regexp">/html&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;?php</span></span><br><span class="line"><span class="regexp">$server = new swoole_websocket_server("0.0.0.0", 9502);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$server-&gt;on('workerStart', function ($server, $workerId) &#123;</span></span><br><span class="line"><span class="regexp">    $client = new swoole_redis;</span></span><br><span class="line"><span class="regexp">    $client-&gt;on('message', function (swoole_redis $client, $result) use ($server) &#123;</span></span><br><span class="line"><span class="regexp">        if ($result[0] == 'message') &#123;</span></span><br><span class="line"><span class="regexp">            var_dump($result);</span></span><br><span class="line"><span class="regexp">            foreach($server-&gt;connections as $fd) &#123;</span></span><br><span class="line"><span class="regexp">                $server-&gt;push($fd, '频道为： ' $result[1] . ' 发送消息：' . $result[2]);</span></span><br><span class="line"><span class="regexp">            &#125;</span></span><br><span class="line"><span class="regexp">        &#125;</span></span><br><span class="line"><span class="regexp">    &#125;);</span></span><br><span class="line"><span class="regexp">    $client-&gt;connect('127.0.0.1', 6379, function (swoole_redis $client, $result) &#123;</span></span><br><span class="line"><span class="regexp">        $client-&gt;subscribe('message');   /</span><span class="regexp">/ 队列名称可以自定义     &#125;);</span></span><br><span class="line"><span class="regexp">&#125;);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$server-&gt;on('open', function ($server, $request) &#123;</span></span><br><span class="line"><span class="regexp">    $server-&gt;push($request-&gt;fd, "hello;\n");</span></span><br><span class="line"><span class="regexp">&#125;);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">/</span><span class="regexp">/ 屏蔽这段会报错 $server-&gt;on('message', function (swoole_websocket_server $server, $request) &#123;</span></span><br><span class="line"><span class="regexp">    $server-&gt;push($request-&gt;fd, "hello");</span></span><br><span class="line"><span class="regexp">&#125;);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$server-&gt;on('close', function ($server, $fd) &#123;</span></span><br><span class="line"><span class="regexp">    echo "client-&#123;$fd&#125; is closed\n";</span></span><br><span class="line"><span class="regexp">    $server-&gt;close($fd);</span></span><br><span class="line"><span class="regexp">&#125;);</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$server-&gt;start();</span></span><br><span class="line"><span class="regexp"> https:/</span><span class="regexp">/alpha2016.github.io/</span><span class="number">2019</span>/<span class="number">02</span>/<span class="number">16</span>/PHP,Swoole,Redis-PubSub%E5%AE%<span class="number">9</span>E%E7%<span class="number">8</span>E%B0%E7%AE%<span class="number">80</span>%E5%<span class="number">8</span>D%<span class="number">95</span>%E6%B6%<span class="number">88</span>%E6%<span class="number">81</span>%AF%E6%<span class="number">8</span>E%A8%E9%<span class="number">80</span>%<span class="number">81</span>/</span><br></pre></td></tr></table></figure>
<h3 id="FFmpeg-截取视频"><a href="#FFmpeg-截取视频" class="headerlink" title="FFmpeg 截取视频"></a>FFmpeg 截取视频</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取文件路径</span></span><br><span class="line">fwrite(STDOUT, <span class="string">"哪个视频文件:"</span>);</span><br><span class="line">$video_path = trim(fgets(STDIN));</span><br><span class="line"></span><br><span class="line">fwrite(STDOUT, <span class="string">"多少秒截取1张图:"</span>);</span><br><span class="line">$s = trim(fgets(STDIN));</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置输出路径</span></span><br><span class="line">fwrite(STDOUT, <span class="string">"输出到哪里:"</span>);</span><br><span class="line">$export_path = trim(fgets(STDIN));</span><br><span class="line"></span><br><span class="line">$command = system(<span class="string">'ffmpeg -y -i '</span>.$video_path.<span class="string">' -vf "fps=1/'</span>.$s.<span class="string">',scale=iw/4:-1,tile=2x2" -an '</span>.$export_path.<span class="string">'_%d.png'</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">// fps = 1/2 每2秒截一张图,如果是每秒截一张 参数就是 fps=1</span></span><br><span class="line"><span class="comment">// scale 截图大小,上面的代码是设置宽为原始的1/4大小,高度自动,也可以设置成固定值如:120:80</span></span><br><span class="line"><span class="comment">// tile 网格化,自动将100张图合并成一张大图 https://learnku.com/laravel/t/33066</span></span><br></pre></td></tr></table></figure>
<h3 id="求两个集合的交集"><a href="#求两个集合的交集" class="headerlink" title="求两个集合的交集"></a>求两个集合的交集</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">$m = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line">$n = [<span class="number">1</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">9</span>];</span><br><span class="line">$intersection = [];</span><br><span class="line"></span><br><span class="line">foreach ($m <span class="keyword">as</span> $m_v) &#123;</span><br><span class="line">    foreach ($n <span class="keyword">as</span> $n_v) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($m_v === $n_v) &#123;</span><br><span class="line">            $intersection = $m_v;<span class="comment">//4</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$m = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line">$n = [<span class="number">1</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">9</span>];</span><br><span class="line">$intersection = [];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取两个数组的长度</span></span><br><span class="line">$length_m = count($m);</span><br><span class="line">$length_n = count($n);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义两个指针</span></span><br><span class="line">$i = $j = <span class="number">0</span>;</span><br><span class="line">$intersection = [];</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> ($i &lt; $length_m &amp;&amp; $j &lt; $length_n) &#123;</span><br><span class="line">    <span class="keyword">if</span> ($m[$i] === $n[$j]) &#123;</span><br><span class="line">        $intersection[] = $m[$i];</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($m[$i] &lt; $n[$j]) &#123;</span><br><span class="line">        $i++;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        $j++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//www.hanjiaxin.com/2018/12/13/algorithm-1/</span></span><br></pre></td></tr></table></figure>
<h3 id="无限级分类"><a href="#无限级分类" class="headerlink" title="无限级分类"></a>无限级分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br></pre></td><td class="code"><pre><span class="line">&amp; 引用赋值</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doloop1</span>(<span class="params">&amp;$i = <span class="number">1</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    print_r($i);</span><br><span class="line">    $i++;</span><br><span class="line">    <span class="keyword">if</span> ($i &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">        doloop1($i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">doloop1();</span><br><span class="line"><span class="keyword">static</span> 静态变量</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doloop2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> $i = <span class="number">1</span>;</span><br><span class="line">    print_r($i);</span><br><span class="line">    $i++;</span><br><span class="line">    <span class="keyword">if</span> ($i &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">        doloop2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">doloop2();</span><br><span class="line">global 全局变量</span><br><span class="line">$i = <span class="number">1</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doloop3</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    global $i;</span><br><span class="line">    echo $i;</span><br><span class="line">    $i++;</span><br><span class="line">    <span class="keyword">if</span> ($i &lt;= <span class="number">10</span>) &#123;</span><br><span class="line">        doloop3();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">doloop3();</span><br><span class="line">    $data = [</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'title'</span> =&gt; <span class="string">'Electronics'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">2</span>, <span class="string">'title'</span> =&gt; <span class="string">'Laptops &amp; PC'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">3</span>, <span class="string">'title'</span> =&gt; <span class="string">'Laptops'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">2</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">4</span>, <span class="string">'title'</span> =&gt; <span class="string">'PC'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">2</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">5</span>, <span class="string">'title'</span> =&gt; <span class="string">'Cameras &amp; photo'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">6</span>, <span class="string">'title'</span> =&gt; <span class="string">'Camera'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">5</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">7</span>, <span class="string">'title'</span> =&gt; <span class="string">'Phones &amp; Accessories'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">8</span>, <span class="string">'title'</span> =&gt; <span class="string">'Smartphones'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">9</span>, <span class="string">'title'</span> =&gt; <span class="string">'Android'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">10</span>, <span class="string">'title'</span> =&gt; <span class="string">'iOS'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">11</span>, <span class="string">'title'</span> =&gt; <span class="string">'Other Smartphones'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">12</span>, <span class="string">'title'</span> =&gt; <span class="string">'Batteries'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">13</span>, <span class="string">'title'</span> =&gt; <span class="string">'Headsets'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">            [<span class="string">'id'</span> =&gt; <span class="number">14</span>, <span class="string">'title'</span> =&gt; <span class="string">'Screen Protectors'</span>, <span class="string">'parent_id'</span> =&gt; <span class="number">7</span>],</span><br><span class="line">        ];</span><br><span class="line">    </span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 值引用获取无限极分类树</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">make_tree</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function"></span>&#123;  </span><br><span class="line">    $refer = array();  </span><br><span class="line">    $tree = array();  </span><br><span class="line">    foreach($data <span class="keyword">as</span> $k =&gt; $v)&#123;  </span><br><span class="line">        $refer[$v[<span class="string">'id'</span>]] = &amp; $data[$k];  <span class="comment">//创建主键的数组引用  </span></span><br><span class="line">    &#125;  </span><br><span class="line"></span><br><span class="line">    foreach($data <span class="keyword">as</span> $k =&gt; $v)&#123;  </span><br><span class="line">        $parent_id = $v[<span class="string">'parent_id'</span>];   <span class="comment">//获取当前分类的父级id  </span></span><br><span class="line">        <span class="keyword">if</span>($parent_id == <span class="number">0</span>)&#123;  </span><br><span class="line">            $tree[] = &amp; $data[$k];   <span class="comment">//顶级栏目</span></span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;  </span><br><span class="line">            <span class="keyword">if</span>(isset($refer[$parent_id]))&#123;  </span><br><span class="line">                $refer[$parent_id][<span class="string">'children'</span>][] = &amp; $data[$k];  <span class="comment">//如果存在父级栏目，则添加进父级栏目的子栏目数组中  </span></span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $tree;  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 递归获取无限极分类树</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @param int $parent_id</span></span><br><span class="line"><span class="comment"> * @param int $level</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">make_tree2</span>(<span class="params">$data = [], $parent_id = <span class="number">0</span>, $level = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $tree = [];</span><br><span class="line">    <span class="keyword">if</span> ($data &amp;&amp; is_array($data)) &#123;</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $v) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($v[<span class="string">'parent_id'</span>] == $parent_id) &#123;</span><br><span class="line">                $tree[] = [</span><br><span class="line">                    <span class="string">'id'</span> =&gt; $v[<span class="string">'id'</span>],</span><br><span class="line">                    <span class="string">'level'</span> =&gt; $level,</span><br><span class="line">                    <span class="string">'title'</span> =&gt; $v[<span class="string">'title'</span>],</span><br><span class="line">                    <span class="string">'parent_id'</span> =&gt; $v[<span class="string">'parent_id'</span>],</span><br><span class="line">                    <span class="string">'children'</span> =&gt; make_tree2($data, $v[<span class="string">'id'</span>], $level + <span class="number">1</span>),</span><br><span class="line">                ];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line">获取子节点以及节点的层级</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 引用赋值方式</span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @param int $id</span></span><br><span class="line"><span class="comment"> * @param int $level</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSubTree</span>(<span class="params">$data = [], $id = <span class="number">0</span>, $level = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> $tree = [];</span><br><span class="line"></span><br><span class="line">    foreach ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($value[<span class="string">'parent_id'</span>] == $id) &#123;</span><br><span class="line">            $value[<span class="string">'laravel'</span>] = $level;</span><br><span class="line">            $tree[] = $value;</span><br><span class="line">            getSubTree($data, $value[<span class="string">'id'</span>], $level + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 静态变量方式</span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @param int $id</span></span><br><span class="line"><span class="comment"> * @param int $level</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSubTree</span>(<span class="params">$data = [], $id = <span class="number">0</span>, $level = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> $tree = [];</span><br><span class="line"></span><br><span class="line">    foreach ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($value[<span class="string">'parent_id'</span>] == $id) &#123;</span><br><span class="line">            $value[<span class="string">'laravel'</span>] = $level;</span><br><span class="line">            $tree[] = $value;</span><br><span class="line">            getSubTree($data, $value[<span class="string">'id'</span>], $level + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 全局变量方式</span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @param int $id</span></span><br><span class="line"><span class="comment"> * @param int $level</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line">$tree = [];  <span class="comment">//先申明变量</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getSubTree</span>(<span class="params">$data = [], $id = <span class="number">0</span>, $level = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    global $tree;</span><br><span class="line"></span><br><span class="line">    foreach ($data <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($value[<span class="string">'parent_id'</span>] == $id) &#123;</span><br><span class="line">            $value[<span class="string">'laravel'</span>] = $level;</span><br><span class="line">            $tree[] = $value;</span><br><span class="line">            getSubTree($data, $value[<span class="string">'id'</span>], $level + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line">通过 pid 获取所有上级分类 常用于面包屑导航</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * getParentsByParentId2($categories, 9)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param array $data</span></span><br><span class="line"><span class="comment"> * @param $parent_id</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getParentsByParentId</span>(<span class="params">$data = [], $parent_id</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">static</span> $categories = [];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($data &amp;&amp; is_array($data)) &#123;</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $item) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($item[<span class="string">'id'</span>] == $parent_id) &#123;</span><br><span class="line">                $categories[] = $item;</span><br><span class="line">                getParentsByParentId($data, $item[<span class="string">'parent_id'</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $categories;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getParentsByParentId2</span>(<span class="params">$data = [], $parent_id</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $categories = [];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($data &amp;&amp; is_array($data)) &#123;</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $item) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($item[<span class="string">'id'</span>] == $parent_id) &#123;</span><br><span class="line">                $categories[] = $item;</span><br><span class="line">                $categories = array_merge($categories, getParentsByParentId2($data, $item[<span class="string">'parent_id'</span>]));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $categories;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/28252</span></span><br></pre></td></tr></table></figure>
<h3 id="MVC-的-“拓展-“模式"><a href="#MVC-的-“拓展-“模式" class="headerlink" title="MVC 的 “拓展 “模式"></a>MVC 的 “拓展 “模式</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">我们把 model 当成 Eloquent <span class="class"><span class="keyword">class</span>，用一个处理数据库逻辑的 <span class="title">Repository</span> 来辅助它，同样对于 <span class="title">controller</span> 来言，它也有一个辅助它的功臣，那就是能处理商业逻辑 <span class="title">Service</span>，这样就解决了臃肿的问题，<span class="title">view</span> 呢？我们是不是忽略了它，并不是的，它也有属于它的处理显示逻辑的 <span class="title">Presenter</span>。</span></span><br><span class="line"><span class="class">由于 <span class="title">SOLID</span> 的单一职责原则与依赖反转原则：我们将数据库逻辑从 <span class="title">model</span> 分离出来，由 <span class="title">repository</span> 辅助 <span class="title">model</span>，将 <span class="title">model</span> 依赖注入进 <span class="title">repository</span>。同样我们将商业逻辑从 <span class="title">controller</span> 分离出来，由 <span class="title">service</span> 辅助 <span class="title">controller</span>，将 <span class="title">service</span> 依赖注入进 <span class="title">controller</span>。显示逻辑也从 <span class="title">view</span> 分离出来，由 <span class="title">presenter</span> 辅助 <span class="title">view</span>，将 <span class="title">presenter</span> 依赖注入进 <span class="title">view</span>。这样写就避免了臃肿和后期维护的不方便。</span></span><br><span class="line">https://learnku.com/articles/33413</span><br></pre></td></tr></table></figure>
<h3 id="window-composer-reuqire"><a href="#window-composer-reuqire" class="headerlink" title="window composer reuqire"></a>window composer reuqire</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> <span class="string">"overtrue/laravel-wechat:~4.0"</span> </span><br><span class="line">实测 Linux 安装 PHP 扩展 pcntl 也可解决。</span><br><span class="line"></span><br><span class="line">如使用 Windows，可composer.json 中增加以下两个字段 config（安装 ext-posix 和 ext-pcntl 的 php7<span class="number">.2</span> 版本扩展）</span><br><span class="line"><span class="string">"platform"</span>: &#123; <span class="string">"ext-pcntl"</span>: <span class="string">"7.2"</span>, <span class="string">"ext-posix"</span>: <span class="string">"7.2"</span>&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/33416</span></span><br></pre></td></tr></table></figure>
<h3 id="位非运算"><a href="#位非运算" class="headerlink" title="位非运算"></a>位非运算</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">echo ~<span class="number">48</span>&amp;~<span class="number">75</span>;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 最好了解下二进制的四则运算和位运算以及运算电路的相关涉及知识^_^</span></span><br><span class="line"><span class="comment"> * 48的二进制值是：00110000 十六进制值是：ffffff30【我系统是64位的输出的结果就是这样】</span></span><br><span class="line"><span class="comment"> * 其实呢计算机内存在存储数据的时候会把负数转换为补码存储【正数的原码，反码，补码完全一样】</span></span><br><span class="line"><span class="comment"> * 而负数呢最高位为1用于表示负数，它的反码呢和正数是相反的，需要将0转换为1，1转换为0，补码操作就是加1操作</span></span><br><span class="line"><span class="comment"> * 当然不同位数的内存长度不一样</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * 75的二进制值是：01001011[我按8位算] 【如果是16位的就是：11111111 01001011，32位，64位的也是同样的道理】</span></span><br><span class="line"><span class="comment"> * echo ~48;//结果为-49</span></span><br><span class="line"><span class="comment"> *          //1、转换为二进制时结果为：00110000【64位系统十六进制就是ffffff30】</span></span><br><span class="line"><span class="comment"> *          //2、此时反码为：11001111【64位系统十六进制就是ffffffcf】</span></span><br><span class="line"><span class="comment"> *          //3、减1操作为：11001110 【64位系统十六进制就是ffffffce】【11111111 11111111 11111111 11001110】</span></span><br><span class="line"><span class="comment"> *          //4、还原为：00110001【64位系统十六进制就是80000031】【二进制为：10000000  00000000 00000000 00110001】</span></span><br><span class="line"><span class="comment"> *          //5、二进制求和结果为：1+16+32=49【注意二进制的最高位1表示负数】输出的结果是：-49</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * echo ~75;//结果为-76，一样的套路</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * echo ~48&amp;~75;//结果却是124</span></span><br><span class="line"><span class="comment"> * 1、48的反码结果为：1100 1111</span></span><br><span class="line"><span class="comment"> * 2、75的的码结果为：1011 0100</span></span><br><span class="line"><span class="comment"> * 3、它们俩相与结果为：1000 0100  【十六进制为：ffffff84】【减1操作后为ffffff83】【自己想想十六进制如何数数^_^】</span></span><br><span class="line"><span class="comment"> * 4、还原后为：0111 1100【十六进制为：8000007c】【二进制结果为：10000000  00000000 00000000 01111100】</span></span><br><span class="line"><span class="comment"> * 5、求和结果为：-124</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">它的结果是：<span class="number">-124</span> </span><br><span class="line">https:<span class="comment">//learnku.com/articles/33382</span></span><br></pre></td></tr></table></figure>
<h3 id="php-html-to-pdf-image"><a href="#php-html-to-pdf-image" class="headerlink" title="php html to pdf image"></a>php html to pdf image</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/krakjoe/wkhtmltox</span></span><br><span class="line">cd wkhtmltox</span><br><span class="line">phpize</span><br><span class="line"><span class="comment">// 这里因为每个人安装环境不一样 /path/to/wkhtmltox/installation 请将这个地址替为wkhtmltox真实的安装路径</span></span><br><span class="line"> <span class="comment">/// PATH 替换为真实的php配置路径</span></span><br><span class="line">./configure --<span class="keyword">with</span>-wkhtmltox=<span class="regexp">/path/</span>to/wkhtmltox/installation --<span class="keyword">with</span>-php-config = PATH</span><br><span class="line">make</span><br><span class="line">make install</span><br><span class="line">将生成的 wkhtmltox.so 添加到 php.ini 的配置中，重启 php</span><br><span class="line">use wkhtmltox\PDF\Converter <span class="keyword">as</span> PDFConverter;</span><br><span class="line">use wkhtmltox\PDF\<span class="built_in">Object</span> <span class="keyword">as</span> PDFObject;</span><br><span class="line"></span><br><span class="line">$converter = <span class="keyword">new</span> PDFConverter([</span><br><span class="line">    <span class="string">"out"</span> =&gt; <span class="string">"test.pdf"</span></span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line">$converter-&gt;add(<span class="keyword">new</span> PDFObject(</span><br><span class="line">    file_get_contents(<span class="string">"http://www.baidu.com"</span>)));</span><br><span class="line"></span><br><span class="line">$converter-&gt;convert();</span><br><span class="line"></span><br><span class="line">use wkhtmltox\Image\Converter <span class="keyword">as</span> ImageConverter;</span><br><span class="line"><span class="comment">// 这里第一个参数可以传入 ` html ` 的字符串</span></span><br><span class="line">$converter = <span class="keyword">new</span> ImageConverter(<span class="literal">null</span>, [</span><br><span class="line">    <span class="string">"fmt"</span> =&gt; <span class="string">"png"</span>,</span><br><span class="line">    <span class="string">"in"</span> =&gt; <span class="string">"http://www.baidu.com"</span>,</span><br><span class="line">    <span class="string">"out"</span> =&gt; <span class="string">"test.png"</span></span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line">$converter = <span class="keyword">new</span> ImageConverter($str, [</span><br><span class="line">    <span class="string">"fmt"</span> =&gt; <span class="string">"jpg"</span>,</span><br><span class="line">    <span class="string">"out"</span> =&gt; <span class="string">"test.jpg"</span></span><br><span class="line">]);</span><br><span class="line">$converter-&gt;convert();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/13472/teach-you-how-to-use-php-to-generate-pdf-and-image-gracefully</span></span><br></pre></td></tr></table></figure>
<h3 id="控制反转，依赖注入"><a href="#控制反转，依赖注入" class="headerlink" title="控制反转，依赖注入"></a>控制反转，依赖注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义写日志的接口规范</span></span><br><span class="line">interface Log</span><br><span class="line">&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">write</span>(<span class="params"></span>);   </span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">// 文件记录日志</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">FileLog</span> <span class="title">implements</span> <span class="title">Log</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">write</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'file log write...'</span>;</span><br><span class="line">    &#125;   </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 数据库记录日志</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DatabaseLog</span> <span class="title">implements</span> <span class="title">Log</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">write</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'database log write...'</span>;</span><br><span class="line">    &#125;   </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 程序操作类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> </span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $fileLog;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;fileLog = <span class="keyword">new</span> FileLog();   </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">login</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 登录成功，记录登录日志</span></span><br><span class="line">        echo <span class="string">'login success...'</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;fileLog-&gt;write();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$user = <span class="keyword">new</span> User();</span><br><span class="line">$user-&gt;login();</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> </span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $log;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Log $log</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;log = $log;   </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">login</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 登录成功，记录登录日志</span></span><br><span class="line">        echo <span class="string">'login success...'</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;log-&gt;write();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$user = <span class="keyword">new</span> User(<span class="keyword">new</span> DatabaseLog());</span><br><span class="line">$user-&gt;login();</span><br><span class="line"></span><br><span class="line"><span class="comment">// routes/web.php</span></span><br><span class="line">Route::get(<span class="string">'/post/store'</span>, <span class="string">'PostController@store'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// App\Http\Controllers</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PostController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">store</span>(<span class="params">Illuminate\Http\Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;validate($request, [</span><br><span class="line">            <span class="string">'category_id'</span> =&gt; <span class="string">'required'</span>,</span><br><span class="line">            <span class="string">'title'</span> =&gt; <span class="string">'required|max:255|min:4'</span>,</span><br><span class="line">            <span class="string">'body'</span> =&gt; <span class="string">'required|min:6'</span>,</span><br><span class="line">        ]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取User的reflectionClass对象</span></span><br><span class="line">$reflector = <span class="keyword">new</span> reflectionClass(User::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 拿到User的构造函数</span></span><br><span class="line">$<span class="keyword">constructor</span> = $reflector-&gt;getConstructor();</span><br><span class="line"></span><br><span class="line">// 拿到User的构造函数的所有依赖参数</span><br><span class="line">$dependencies = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line"></span><br><span class="line">// 创建user对象</span><br><span class="line">$user = $reflector-&gt;newInstance();</span><br><span class="line"></span><br><span class="line">// 创建user对象，需要传递参数的</span><br><span class="line">$user = $reflector-&gt;newInstanceArgs($dependencies = []);</span><br><span class="line">这时候我们可以创建一个 make 方法，传入 User，利用反射机制拿到 User 的构造函数，进而得到构造函数的参数对象。用递归的方式创建参数依赖。最后调用 newInstanceArgs 方法生成 User 实例。 可能有些同学还不是很理解。下面我们用代码去简单模拟下</span><br><span class="line"></span><br><span class="line">function make($concrete)&#123;</span><br><span class="line">    <span class="comment">// 或者User的反射类</span></span><br><span class="line">    $reflector = <span class="keyword">new</span> ReflectionClass($concrete);</span><br><span class="line">    <span class="comment">// User构造函数</span></span><br><span class="line">    $<span class="keyword">constructor</span> = $reflector-&gt;getConstructor();</span><br><span class="line">    // User构造函数参数</span><br><span class="line">    $dependencies = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line">    // 最后生成User    </span><br><span class="line">    return $reflector-&gt;newInstanceArgs($dependencies);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$user = make('User');</span><br><span class="line">$user-&gt;login();</span><br><span class="line"></span><br><span class="line">// 注意我们这里需要修改一下User的构造函数，如果不去修改。反射是不能动态创建接口的，那如果非要用接口该怎么处理呢？下一节我们讲Ioc容器的时候会去解决。</span><br><span class="line"></span><br><span class="line">class User </span><br><span class="line">&#123;</span><br><span class="line">    protected $log;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">FileLog $log</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;log = $log;   </span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">login</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 登录成功，记录登录日志</span></span><br><span class="line">        echo <span class="string">'login success...'</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;log-&gt;write();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$concrete</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    $reflector = <span class="keyword">new</span> ReflectionClass($concrete);</span><br><span class="line">    $<span class="keyword">constructor</span> = $reflector-&gt;getConstructor();</span><br><span class="line">    // 为什么这样写的? 主要是递归。比如创建FileLog不需要传入参数。</span><br><span class="line">    if(is_null($<span class="keyword">constructor</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $reflector-&gt;newInstance();</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 构造函数依赖的参数</span></span><br><span class="line">        $dependencies = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line">        // 根据参数返回实例，如FileLog</span><br><span class="line">        $instances = getDependencies($dependencies);</span><br><span class="line">        return $reflector-&gt;newInstanceArgs($instances);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function getDependencies($paramters) &#123;</span><br><span class="line">    $dependencies = [];</span><br><span class="line">    foreach ($paramters <span class="keyword">as</span> $paramter) &#123;</span><br><span class="line">        $dependencies[] = make($paramter-&gt;getClass()-&gt;name);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $dependencies;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$user = make(<span class="string">'User'</span>);</span><br><span class="line">$user-&gt;login();</span><br><span class="line">https:<span class="comment">//www.tinker777.com/show/12</span></span><br></pre></td></tr></table></figure>
<h3 id="英文字符占-0-5-个，中文字符占-1-个"><a href="#英文字符占-0-5-个，中文字符占-1-个" class="headerlink" title="英文字符占 0.5 个，中文字符占 1 个"></a>英文字符占 0.5 个，中文字符占 1 个</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 计算单字节.</span></span><br><span class="line">    preg_match_all(<span class="string">'/[a-zA-Z0-9_]/'</span>, $value, $single);</span><br><span class="line">    $single = count($single[<span class="number">0</span>]) / <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 多子节长度.</span></span><br><span class="line">    $double = mb_strlen(preg_replace(<span class="string">'([a-zA-Z0-9_])'</span>, <span class="string">''</span>, $value));</span><br><span class="line"></span><br><span class="line">    $length = $single + $double;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">str_display_len</span> (<span class="params">string $str</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        preg_match_all(<span class="string">'/[a-zA-Z0-9_]/'</span>, $str, $single);</span><br><span class="line">    </span><br><span class="line">        <span class="keyword">return</span> count($single[<span class="number">0</span>]) / <span class="number">2</span> + mb_strlen(preg_replace(<span class="string">'([a-zA-Z0-9_])'</span>, <span class="string">''</span>, $str));</span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">the_fucking_length_function</span>(<span class="params">$str</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> strlen(mb_convert_encoding($str, <span class="string">'GB2312'</span>)) / <span class="number">2</span>;</span><br><span class="line">    <span class="comment">//$length = (strlen($value) + mb_strlen($value)) / 2;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mbstrlen</span>(<span class="params">$str</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> ceil((strlen($str) + mb_strlen($str, <span class="string">"UTF8"</span>)) / <span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"> mb_strwidth (<span class="string">'中国123'</span>,<span class="string">'utf-8'</span>)<span class="comment">//7</span></span><br><span class="line">https:<span class="comment">//learnku.com/articles/4956/following-my-unique-needs-of-the-english-characters-accounted-for-05-chinese-characters-accounted-for-1</span></span><br><span class="line">https:<span class="comment">//www.php.net/manual/zh/function.mb-strwidth.php</span></span><br></pre></td></tr></table></figure>
<h3 id="支付宝-App-第三方登录获取-用户信息"><a href="#支付宝-App-第三方登录获取-用户信息" class="headerlink" title="支付宝 App 第三方登录获取 用户信息"></a>支付宝 App 第三方登录获取 用户信息</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * AlipayToken 获得用户 请求token, 通过它获得 用户信息</span></span><br><span class="line"><span class="comment">  * </span></span><br><span class="line"><span class="comment">  * 需要按照支付宝加签流程来。</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">userInfo</span>(<span class="params">$app_auth_token</span>)</span></span><br><span class="line"><span class="function"> </span>&#123;</span><br><span class="line">     $infoArr = [</span><br><span class="line">         <span class="string">'method'</span> =&gt; <span class="string">'alipay.system.oauth.token'</span>,</span><br><span class="line">         <span class="string">'app_id'</span> =&gt; $<span class="keyword">this</span>-&gt;app_id,</span><br><span class="line">         <span class="string">'charset'</span> =&gt; <span class="string">'utf-8'</span>,</span><br><span class="line">         <span class="string">'sign_type'</span> =&gt; <span class="string">'RSA2'</span>,</span><br><span class="line">         <span class="string">'timestamp'</span> =&gt; date(<span class="string">'Y-m-d H:i:s'</span>),</span><br><span class="line">         <span class="string">'version'</span> =&gt; <span class="string">'1.0'</span>,</span><br><span class="line">         <span class="string">'code'</span> =&gt; $app_auth_token,</span><br><span class="line">         <span class="string">'grant_type'</span> =&gt; <span class="string">'authorization_code'</span>,</span><br><span class="line">     ];</span><br><span class="line"></span><br><span class="line">     $signStr = $<span class="keyword">this</span>-&gt;myHttpBuildQuery($infoArr);</span><br><span class="line">     $sign = urlencode($<span class="keyword">this</span>-&gt;enRSA2($signStr));</span><br><span class="line">     $qureStr = $signStr.<span class="string">'&amp;sign='</span>.$sign;</span><br><span class="line"></span><br><span class="line">     $res = <span class="keyword">new</span> Client();</span><br><span class="line">     $body = $res-&gt;get(<span class="string">'https://openapi.alipay.com/gateway.do?'</span>.$qureStr)-&gt;getBody()-&gt;getContents();</span><br><span class="line">     $body = json_decode($body);</span><br><span class="line">     <span class="keyword">if</span> (!isset($body-&gt;alipay_system_oauth_token_response-&gt;access_token)) &#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="string">'接口异常'</span>;</span><br><span class="line">     &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">         $autho_token = $body-&gt;alipay_system_oauth_token_response-&gt;access_token;</span><br><span class="line">         $userinfo = $<span class="keyword">this</span>-&gt;aliPayUserInfo($autho_token);</span><br><span class="line">         <span class="keyword">return</span> $userinfo; <span class="comment">// 或则 返回 json_encode($userinfo) 根据实际需求来 </span></span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">  * AliPayUserInfo 通过 token 获取用户信息</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> private <span class="function"><span class="keyword">function</span> <span class="title">aliPayUserInfo</span>(<span class="params">$autho_token</span>)</span></span><br><span class="line"><span class="function"> </span>&#123;</span><br><span class="line">     $infoArr = [</span><br><span class="line">         <span class="string">'method'</span> =&gt; <span class="string">'alipay.user.info.share'</span>,</span><br><span class="line">         <span class="string">'app_id'</span> =&gt; $<span class="keyword">this</span>-&gt;app_id,</span><br><span class="line">         <span class="string">'charset'</span> =&gt; <span class="string">'utf-8'</span>,</span><br><span class="line">         <span class="string">'sign_type'</span> =&gt; <span class="string">'RSA2'</span>,</span><br><span class="line">         <span class="string">'timestamp'</span> =&gt; date(<span class="string">'Y-m-d H:i:s'</span>),</span><br><span class="line">         <span class="string">'version'</span> =&gt; <span class="string">'1.0'</span>,</span><br><span class="line">         <span class="string">'auth_token'</span> =&gt; $autho_token,</span><br><span class="line">     ];</span><br><span class="line"></span><br><span class="line">     $signStr = $<span class="keyword">this</span>-&gt;myHttpBuildQuery($infoArr);</span><br><span class="line">     $sign = urlencode($<span class="keyword">this</span>-&gt;enRSA2($signStr));</span><br><span class="line">     $qureStr = $signStr.<span class="string">'&amp;sign='</span>.$sign;</span><br><span class="line"></span><br><span class="line">     $res = <span class="keyword">new</span> Client();</span><br><span class="line">     $body = $res-&gt;get(<span class="string">'https://openapi.alipay.com/gateway.do?'</span>.$qureStr)-&gt;getBody()-&gt;getContents();</span><br><span class="line">     $body = json_decode($body);</span><br><span class="line">     <span class="keyword">if</span> (!isset($body-&gt;alipay_user_info_share_response)) &#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="string">'接口异常'</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     $body = $body-&gt;alipay_user_info_share_response;</span><br><span class="line">     <span class="keyword">return</span> $body;</span><br><span class="line"> &#125;</span><br><span class="line"> https:<span class="comment">//learnku.com/articles/30076#replies</span></span><br></pre></td></tr></table></figure>
<h3 id="树状数据结构存储方式"><a href="#树状数据结构存储方式" class="headerlink" title="树状数据结构存储方式"></a>树状数据结构存储方式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE <span class="string">`categories`</span> (</span><br><span class="line">  <span class="string">`id`</span> int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`title`</span> char(<span class="number">100</span>) NOT NULL,</span><br><span class="line">  <span class="string">`pid`</span> int(<span class="number">11</span>) DEFAULT <span class="number">0</span>,</span><br><span class="line">  PRIMARY KEY (<span class="string">`id`</span>)</span><br><span class="line">) ENGINE=InnoDB;</span><br><span class="line">CREATE TABLE <span class="string">`categories`</span> (</span><br><span class="line">  <span class="string">`id`</span> int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`title`</span> char(<span class="number">100</span>) NOT NULL,</span><br><span class="line">  <span class="string">`lft`</span> int(<span class="number">11</span>) NOT NULL UNIQUE CHECK (lft&gt; <span class="number">0</span>),</span><br><span class="line">  <span class="string">`rgt`</span> int(<span class="number">11</span>) NOT NULL UNIQUE CHECK (rgt&gt; <span class="number">1</span>),</span><br><span class="line">  PRIMARY KEY (<span class="string">`id`</span>)</span><br><span class="line">) ENGINE=InnoDB;</span><br><span class="line">lft 和 rgt 是作为集合的边界，两者差值越大，则集合越大，里面的元素就越多。</span><br><span class="line">根据子集，查找父级的分类</span><br><span class="line"></span><br><span class="line">SELECT c2.* </span><br><span class="line">  FROM categories <span class="keyword">as</span> c1, categories <span class="keyword">as</span> c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft and c2.rgt </span><br><span class="line">      AND c1.title = <span class="string">'华为'</span>;</span><br><span class="line"></span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">| id | title       | lft | rgt |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">|  <span class="number">1</span> | Smartphones |   <span class="number">1</span> |  <span class="number">14</span> |</span><br><span class="line">|  <span class="number">5</span> | Harmony OS  |  <span class="number">10</span> |  <span class="number">13</span> |</span><br><span class="line">|  <span class="number">8</span> | 华为        |  <span class="number">11</span> |  <span class="number">12</span> |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">根据父级，查找其底下所有的子集</span><br><span class="line"></span><br><span class="line">SELECT c1.*</span><br><span class="line">   FROM categories AS c1, categories AS c2</span><br><span class="line">  WHERE c1.lft BETWEEN c2.lft AND c2.rgt</span><br><span class="line">    AND c2.title = <span class="string">'Smartphones'</span>;</span><br><span class="line"></span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">| id | title       | lft | rgt |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">|  <span class="number">1</span> | Smartphones |   <span class="number">1</span> |  <span class="number">14</span> |</span><br><span class="line">|  <span class="number">3</span> | Android     |   <span class="number">2</span> |   <span class="number">5</span> |</span><br><span class="line">|  <span class="number">4</span> | iOS         |   <span class="number">6</span> |   <span class="number">9</span> |</span><br><span class="line">|  <span class="number">5</span> | Harmony OS  |  <span class="number">10</span> |  <span class="number">13</span> |</span><br><span class="line">|  <span class="number">6</span> | 小米        |   <span class="number">3</span> |   <span class="number">4</span> |</span><br><span class="line">|  <span class="number">7</span> | iPhone      |   <span class="number">7</span> |   <span class="number">8</span> |</span><br><span class="line">|  <span class="number">8</span> | 华为        |  <span class="number">11</span> |  <span class="number">12</span> |</span><br><span class="line">+----+-------------+-----+-----+</span><br><span class="line">查看各个分类的级别</span><br><span class="line"></span><br><span class="line"> SELECT COUNT(c2.id) AS indentation, c1.title</span><br><span class="line">  FROM categories AS c1, categories AS c2下周三we<span class="string">'fv</span></span><br><span class="line"><span class="string">  WHERE c1.lft BETWEEN c2.lft AND c2.rgt</span></span><br><span class="line"><span class="string">  GROUP BY c1.title</span></span><br><span class="line"><span class="string">  ORDER BY c1.lft;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">+-------------+-------------+</span></span><br><span class="line"><span class="string">| indentation | title       |</span></span><br><span class="line"><span class="string">+-------------+-------------+</span></span><br><span class="line"><span class="string">|           1 | Smartphones |</span></span><br><span class="line"><span class="string">|           2 | Android     |</span></span><br><span class="line"><span class="string">|           3 | 小米        |</span></span><br><span class="line"><span class="string">|           2 | iOS         |</span></span><br><span class="line"><span class="string">|           3 | iPhone      |</span></span><br><span class="line"><span class="string">|           2 | Harmony OS  |</span></span><br><span class="line"><span class="string">|           3 | 华为        |</span></span><br><span class="line"><span class="string">+-------------+-------------+</span></span><br><span class="line"><span class="string">https://learnku.com/articles/33630</span></span><br></pre></td></tr></table></figure>
<p><a href="https://github.com/FireLustre/php-dfa-sensitive" target="_blank" rel="noopener">敏感词过滤 </a></p>
<p><a href="https://learnku.com/articles/33646" target="_blank" rel="noopener">PHP 中基于 Casbin 做 RBAC + RESTful 权限控制</a></p>
<p><a href="https://github.com/k9606/kxcel" target="_blank" rel="noopener">方便导入和导出 Excel</a></p>
<p><a href="https://learnku.com/articles/33537" target="_blank" rel="noopener">windows10 swoole laravel</a></p>
<p><a href="https://learnku.com/articles/25440" target="_blank" rel="noopener">Windows 系统下的 Linux 子系统</a></p>
<p><a href="https://learnku.com/articles/30407" target="_blank" rel="noopener">【系统】打造自己最喜爱的 Windows10 —— U 盘启动盘制作篇</a></p>
<p><a href="https://learnku.com/articles/32848" target="_blank" rel="noopener"> sentry 追踪 Laravel 生产环境 bug</a></p>
<p><a href="https://learnku.com/articles/25478" target="_blank" rel="noopener">尝试 Leetcode</a></p>
<p><a href="https://github.com/aliliin/LeetCode-Algorithms" target="_blank" rel="noopener">LeetCode-Algorithms 算法题 PHP 实现</a></p>
<p><a href="https://github.com/aliliin/LeetCode-database-problemset" target="_blank" rel="noopener">Leetcode database problem 数据库练习题</a></p>
<p><a href="https://learnku.com/articles/28607" target="_blank" rel="noopener">PHP 与 Swoole 浅析与学习</a></p>
<p><a href="https://laragon.org/" target="_blank" rel="noopener">PHP开发环境laragon</a></p>
<p><a href="https://www.easyswoole.com/Cn/Video/income.html" target="_blank" rel="noopener">php swoole视频</a></p>
<p><a href="https://learnku.com/articles/13472/teach-you-how-to-use-php-to-generate-pdf-and-image-gracefully" target="_blank" rel="noopener">教你如何使用 PHP 优雅的生成 PDF 和 Image</a></p>
<p><a href="https://learnku.com/articles/25922?order_by=vote_count&amp;" target="_blank" rel="noopener">使用 Laravel-snappy 生成 PDF 踩坑记录</a></p>
<p><a href="https://segmentfault.com/a/1190000018765575" target="_blank" rel="noopener">初识wkhtmltopdf</a></p>
<p><a href="https://github.com/zmecust/learning-manual" target="_blank" rel="noopener">个人学习记录</a></p>
<p><a href="https://www.phpliveregex.com/" target="_blank" rel="noopener">PHP 的在线正则表达式</a></p>
<p><a href="https://alpha2016.github.io/2019/03/06/%E4%BC%98%E5%8C%96Nginx%E5%92%8CPHP-fpm%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/" target="_blank" rel="noopener">优化Nginx及PHP-fpm的几种方式</a></p>
<p><a href="https://learnku.com/articles/33189" target="_blank" rel="noopener">php注释标记</a></p>
<p><a href="https://php1024.com/posts/45.htm" target="_blank" rel="noopener">PHP使用Dompdf</a></p>
<p><a href="https://learnku.com/articles/33320" target="_blank" rel="noopener">CentOS 下多版本 PHP 的安装与配置</a></p>
<p><a href="https://github.com/jesseduffield/lazydocker" target="_blank" rel="noopener">终端显示你的 docker 状态</a></p>
<p><a href="https://learnku.com/articles/32416" target="_blank" rel="noopener">复杂度分享</a></p>
<p><a href="https://learnku.com/articles/32025" target="_blank" rel="noopener">Laravel-admin 源码分析系列</a></p>
<p><a href="https://learnku.com/articles/32699" target="_blank" rel="noopener">Laravel 如何实现数据的软删除</a></p>
<p><a href="https://github.com/alicfeng/TeamStandard/tree/master/book/PHP" target="_blank" rel="noopener">PHP开发规范单元测试</a></p>
<p><a href="http://cuihuan.net/2019/05/12/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF/" target="_blank" rel="noopener">字符编码那些事儿</a></p>
<p><a href="https://github.com/alicfeng/TeamStandard" target="_blank" rel="noopener">基于 gitbook 构建团队项目开发规范文档</a></p>
<p><a href="https://post.zz173.com/detail/lq150Y4PnNNnyG_RwtyRCw.html" target="_blank" rel="noopener">PHP深入pack/unpack</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/12/那些你可能用得上的简历写作工具/" rel="next" title="那些你可能用得上的简历写作工具">
                <i class="fa fa-chevron-left"></i> 那些你可能用得上的简历写作工具
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/19/如何在豆瓣租房小组快速找到满意的房子/" rel="prev" title="如何在豆瓣租房小组快速找到满意的房子">
                如何在豆瓣租房小组快速找到满意的房子 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#不需要递归"><span class="nav-number">1.</span> <span class="nav-text">不需要递归</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#插入排序算法"><span class="nav-number">2.</span> <span class="nav-text">插入排序算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分表"><span class="nav-number">3.</span> <span class="nav-text">分表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sentry-升级"><span class="nav-number">4.</span> <span class="nav-text">sentry 升级</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于雪花算法的-PHP-ID-生成器"><span class="nav-number">5.</span> <span class="nav-text">基于雪花算法的 PHP ID 生成器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#内置web服务器"><span class="nav-number">6.</span> <span class="nav-text">内置web服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#响应宏"><span class="nav-number">7.</span> <span class="nav-text">响应宏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Intervention-Image-合成图片"><span class="nav-number">8.</span> <span class="nav-text">Intervention Image 合成图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx-配置多域名"><span class="nav-number">9.</span> <span class="nav-text">nginx 配置多域名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#插入折半排序"><span class="nav-number">10.</span> <span class="nav-text">插入折半排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sql拼接"><span class="nav-number">11.</span> <span class="nav-text">sql拼接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#x-www-form-urlencoded-与-multipart-form-data"><span class="nav-number">12.</span> <span class="nav-text">x-www-form-urlencoded 与 multipart/form-data</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Model-setAttribute"><span class="nav-number">13.</span> <span class="nav-text">Model::setAttribute()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#处理unicode解码"><span class="nav-number">14.</span> <span class="nav-text">处理unicode解码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#恶意提交攻击"><span class="nav-number">15.</span> <span class="nav-text">恶意提交攻击</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#切换镜像"><span class="nav-number">16.</span> <span class="nav-text">切换镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-Failed-to-execute-unzip"><span class="nav-number">17.</span> <span class="nav-text">composer Failed to execute unzip</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sentry"><span class="nav-number">18.</span> <span class="nav-text">sentry</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#抽离-vendor"><span class="nav-number">19.</span> <span class="nav-text">抽离 vendor</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#dd-函数"><span class="nav-number">20.</span> <span class="nav-text">dd 函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#listen-sql"><span class="nav-number">21.</span> <span class="nav-text">listen sql</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#模型事件-updated"><span class="nav-number">22.</span> <span class="nav-text">模型事件 updated</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取所有-collection"><span class="nav-number">23.</span> <span class="nav-text">获取所有 collection</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取带参数的-sql-语句"><span class="nav-number">24.</span> <span class="nav-text">获取带参数的 sql 语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#命令行输出进度条"><span class="nav-number">25.</span> <span class="nav-text">命令行输出进度条</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#with-通过闭包筛选"><span class="nav-number">26.</span> <span class="nav-text">with 通过闭包筛选</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-cookie加密解密"><span class="nav-number">27.</span> <span class="nav-text">Laravel cookie加密解密</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Eloquent-Builder-添加自定义方法"><span class="nav-number">28.</span> <span class="nav-text">Eloquent Builder 添加自定义方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改模型对象的某个属性"><span class="nav-number">29.</span> <span class="nav-text">修改模型对象的某个属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel技巧"><span class="nav-number">30.</span> <span class="nav-text">laravel技巧</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#外部调用类的私有方法"><span class="nav-number">31.</span> <span class="nav-text">外部调用类的私有方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#30-分钟未付款自动取消订单"><span class="nav-number">32.</span> <span class="nav-text">30 分钟未付款自动取消订单</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#guzzle异步请求"><span class="nav-number">33.</span> <span class="nav-text">guzzle异步请求</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除数组元素"><span class="nav-number">34.</span> <span class="nav-text">删除数组元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何查Session内容"><span class="nav-number">35.</span> <span class="nav-text">如何查Session内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#redis队列缓存"><span class="nav-number">36.</span> <span class="nav-text">redis队列缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel服务容器"><span class="nav-number">37.</span> <span class="nav-text">laravel服务容器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#反射动态代理"><span class="nav-number">38.</span> <span class="nav-text">反射动态代理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装"><span class="nav-number">39.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#phpstorm-gitbash"><span class="nav-number">40.</span> <span class="nav-text">phpstorm gitbash</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#composer-找不到文件"><span class="nav-number">41.</span> <span class="nav-text">composer 找不到文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分组查询"><span class="nav-number">42.</span> <span class="nav-text">分组查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#取k个不重复的数"><span class="nav-number">43.</span> <span class="nav-text">取k个不重复的数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Intervention-Image-库处理图片"><span class="nav-number">44.</span> <span class="nav-text">Intervention Image 库处理图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-PubSub实现简单消息推送"><span class="nav-number">45.</span> <span class="nav-text">Redis PubSub实现简单消息推送</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FFmpeg-截取视频"><span class="nav-number">46.</span> <span class="nav-text">FFmpeg 截取视频</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#求两个集合的交集"><span class="nav-number">47.</span> <span class="nav-text">求两个集合的交集</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无限级分类"><span class="nav-number">48.</span> <span class="nav-text">无限级分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MVC-的-“拓展-“模式"><span class="nav-number">49.</span> <span class="nav-text">MVC 的 “拓展 “模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#window-composer-reuqire"><span class="nav-number">50.</span> <span class="nav-text">window composer reuqire</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#位非运算"><span class="nav-number">51.</span> <span class="nav-text">位非运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-html-to-pdf-image"><span class="nav-number">52.</span> <span class="nav-text">php html to pdf image</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#控制反转，依赖注入"><span class="nav-number">53.</span> <span class="nav-text">控制反转，依赖注入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#英文字符占-0-5-个，中文字符占-1-个"><span class="nav-number">54.</span> <span class="nav-text">英文字符占 0.5 个，中文字符占 1 个</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#支付宝-App-第三方登录获取-用户信息"><span class="nav-number">55.</span> <span class="nav-text">支付宝 App 第三方登录获取 用户信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#树状数据结构存储方式"><span class="nav-number">56.</span> <span class="nav-text">树状数据结构存储方式</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
